JAVAFX add dynamic element in a FXML Gridpane


Question

In my FXML I created a gridpane. Now I want to add dynamic element (Like button, textfield) by java code (not by FXML), while I am trying to do so, I am getting error. Please Help.

my FXML:

    <AnchorPane fx:controller="tableview.TableViewSample" id="AnchorPane" maxHeight="-     Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml">
  <children>
    <GridPane fx:id="greadpane" layoutX="0.0" layoutY="0.0" prefHeight="400.0" prefWidth="600.0">
      <columnConstraints>
        <ColumnConstraints fx:id="col0" hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
      </columnConstraints>
      <rowConstraints>
        <RowConstraints  fx:id="row0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      </rowConstraints>
    </GridPane>
  </children>
    </AnchorPane> 

My Java Code:

    public class TableViewSample extends Application {

    @FXML private GridPane greadpane;
  public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws IOException {

        Pane myPane = (Pane)FXMLLoader.load(getClass().getResource
                ("tabviewexamlpe.fxml"));
        Scene scene = new Scene(myPane);
        stage.setTitle("Table View ");
        stage.setWidth(450);
        stage.setHeight(500);
        stage.setScene(scene);       

        final Label label = new Label("Address Book");
        label.setFont(new Font("Arial", 20));
        greadpane.add(label, 0, 0);
        stage.show();
}
}
1
4
6/23/2013 2:48:03 PM

Accepted Answer

You get a null pointer cause you try to do operation before stage.show() so fxml are not yet initialized. don't do dirty things and put your greadPane.add on a separate controller

public class Controller implements Initializable {

    @FXML
    private GridPane greadpane;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        final Label label = new Label("Address Book");
        label.setFont(new Font("Arial", 20));
        greadpane.add(label, 0, 0);
    }
}

and assign your fxml to this controler. and it's will be ok

12
6/23/2013 3:27:31 PM

I ran into the same problem and used Agonist_ advice, however instead of separating the gridPane into a new controller i just created a thread that runs 10ms later to execute the code which is waiting for stage.show().

public GameController(Game game) {
    game.addObserver(this);
    new Thread() {
        @Override
        public void run() {
            try {
                Thread.sleep(10);
                Platform.runLater(() -> {
                    game.startBeginnerRound();
                });
            } catch (InterruptedException ex) {
                Logger.getLogger(GameController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }.start();
}

In this example the gridPane is updated when the observable notifies it, in this case when game.startBeginnerRound() is executed.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon