2016-11-03 3 views
0

私はJavaFXを使用しています。ボタンをクリックすると、新しいラジオボタンが作成され、フローパネル内に表示されます。これは可能ですか?私はこれをやろうとしましたが、何も現れていないようです。実行時に新しいラジオボタンオプションを作成する

編集: 問題はControllerクラスのinsertDemand()メソッドに起因するようです。メソッドの最初の2行を削除すると、ラジオボタンが表示されます。なぜそれがこれを行うのか分かりません。しかし、ボタンがクリックされて画面に表示される行とラジオボタンが表示されるときに、これらの2行を削除することは実現可能な解決策ではありません。ここで

はFXMLである:ここでは

<GridPane alignment="center" hgap="10" prefHeight="800.0" prefWidth="1200.0" stylesheets="/sample/sample.css" vgap="10" xmlns="http://javafx.com/javafx/8.0.76-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> 
    <children> 
     <!--Insert--> 
     <ScrollPane GridPane.columnIndex="0" GridPane.rowIndex="1" GridPane.rowSpan="4"> 
      <content> 
       <FlowPane> 
        <Button fx:id="demandB" mnemonicParsing="false" onAction="#insertDemand" text="Demand" /> 
       </FlowPane> 
      </content> 
     </ScrollPane> 
     <StackPane fx:id="workspaceSP" GridPane.columnIndex="1" GridPane.rowIndex="0" GridPane.rowSpan="2"> 
     </StackPane> 
     <ScrollPane GridPane.columnIndex="1" GridPane.rowIndex="3"> 
      <content> 
       <FlowPane fx:id="lineButtonsFP" prefHeight="100.0"> 
        <Label text="Select a Curve:" /> 

       </FlowPane> 
      </content> 
     </ScrollPane> 
    </children> 
    <columnConstraints> 
     <ColumnConstraints maxWidth="600.0" minWidth="392.0" prefWidth="400.0" /> 
    </columnConstraints> 
    <rowConstraints> 
     <RowConstraints maxHeight="200.0" minHeight="22.0" prefHeight="39.0" /> 
     <RowConstraints /> 
    </rowConstraints> 
</GridPane> 

は、コントローラクラスです:

public class Controller { 

    static Parent graphMaker; 
    @FXML 
    StackPane workspaceSP; 
    @FXML 
    FlowPane lineButtonsFP; 
    static LinkedList<Demand> curvesLL; 
    ToggleGroup group = new ToggleGroup(); 
    public void insertDemand() { 
     Demand demand = new Demand(workspaceSP,200,200,400,400); 
     curvesLL.add(demand); 
     RadioButton radioButton = new RadioButton("Demand"); 
     radioButton.setToggleGroup(group); 
     radioButton.setSelected(true); 
     lineButtonsFP.getChildren().add(radioButton); 
    } 
} 

需要は私が作ったクラスです。作成されたときに、ラインが表示されます。

public class Demand { 

     public Demand(Pane pane, double startX, double startY, double endX, double endY){ 
      pane.getChildren().add(makeLine(startX, startY, endX, endY)); 
     } 

     private Line makeLine(double startX, double startY, double endX, double endY) { 
      Line line = new Line(startX, startY, endX, endY); 
      return line; 
     } 
    } 

} 

はあなたの助けをありがとう:)

編集2:を私はそれを動作させる方法を発見しました。この問題がなぜ修正されるのか分かりませんが、insertDemand()メソッドの最初の2行を下に移動して、ラジオボタンが最初に作成され、次にDemanオブジェクトが作成されると、それは機能します。

+0

ここで私は専門家はいませんが、実行時にJava APIを使用して新しいラジオボタンを追加する必要があると思います。 – Thomas

+1

私のために働く。 [mcve]を投稿してください。 – fabian

+0

完全なfxml +コントローラクラス(+ fxmlの読み込みに使用されるコード?)は私たちに問題を示しています...私がこの作業をするために唯一行った措置は、 'Button'と' FlowPane'を 'VBox 'を呼び出し、コードスニペットをコントローラに追加します。 BTW:あなたのウィンドウ/ 'FlowPane'の親は、FlowPaneの追加の子を偶然に見せるには小さすぎませんか? – fabian

答えて

0

私は簡単な例を示しました。私はFXMLを気にしませんでしたが、なぜそれが違うのか分かりません。それでも問題が解決しない場合は、問題の例を提示する必要があります。

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.RadioButton; 
import javafx.scene.control.ToggleGroup; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.FlowPane; 
import javafx.stage.Stage; 


public class Main extends Application { 
    @Override 
    public void start(Stage primaryStage) { 
     try { 
      BorderPane root = new BorderPane(); 
      Scene scene = new Scene(root,400,400); 
      FlowPane lineButtonsFP = new FlowPane(); 
      root.setCenter(lineButtonsFP); 
      ToggleGroup group = new ToggleGroup(); 

      Button add = new Button("add"); 
      root.setTop(add); 
      add.setOnAction((action) -> 
      { 
       RadioButton radioButton = new RadioButton("Demand"); 
       radioButton.setToggleGroup(group); 
       radioButton.setSelected(true); 
       lineButtonsFP.getChildren().add(radioButton);     
      }); 
      primaryStage.setScene(scene); 
      primaryStage.show(); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 
+0

デビッドさんに時間を割いてくれてありがとう。私はポストを更新して、何が動作していないのかをさらに見つけようとしました。すべてを賞賛してください! –

関連する問題