0

I have created my view for a JavaFX project using FXML. I was wondering if it's possible to add a few more items to the view pragmatically in controller class?

For example, how can I add another set of Text and TextField in my controller file?

FXML

<AnchorPane prefHeight="400.0" prefWidth="228.0">
   <children>
      <Text layoutY="10.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Selected User:" GridPane.rowIndex="0" />
      <Text layoutY="45.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Username :" GridPane.rowIndex="1" />
      <TextField fx:id="username" layoutY="55.0" GridPane.rowIndex="2" />
      <Text layoutY="100.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Address :" GridPane.rowIndex="3" />
      <TextField fx:id="address" layoutY="110.0" GridPane.rowIndex="4" />
      <Text layoutY="155.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Web Page :" GridPane.rowIndex="5" />
      <TextField fx:id="WP" layoutY="165.0" GridPane.rowIndex="6" />
      <Text layoutY="210.0" strokeType="OUTSIDE" strokeWidth="0.0" text="State :" GridPane.rowIndex="7" />
      <Text layoutY="265.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Login Name :" GridPane.rowIndex="9" />
      <TextField fx:id="login_Name" layoutY="275.0" GridPane.rowIndex="10" />
      <Button fx:id="btnSave" layoutX="50.0" layoutY="320.0" mnemonicParsing="false" onAction="#btnSaveClicked" text="Save" />
      <Button fx:id="btnSave" layoutX="35.0" layoutY="365.0" mnemonicParsing="false" onAction="#btnDeleteUserClicked" text="Delete User" />
      <Button fx:id="btnAddNew" layoutX="30.0" layoutY="410.0" mnemonicParsing="false" onAction="#btnAddeNewClicked" text="Add New User" />
      <TextField fx:id="state" layoutX="-2.0" layoutY="222.0" />
   </children>
</AnchorPane>
5
  • Your question is very generic. Can you specific what exactly are you looking for? FYI, adding items via controller is possible, although, I wouldn't recommend it. Commented Aug 19, 2016 at 8:47
  • @ItachiUchiha I am completing a half done Javafx project which was done by some one else . The previous programmer had decided to create the interface using FXML style. Now I'd like to add a few more items to that interface (Same Window) and I'd rather do it pragmatically . The question is if I want to do so should I build up the whole thing from the very begging or I can continue along with it . ??? :) Commented Aug 19, 2016 at 8:56
  • You can continue from where he left. But, why would you not add the items in FXML? Commented Aug 19, 2016 at 8:57
  • @ItachiUchiha Just self preference . I have better understanding of what I am doing while I do it pragmatically . (But how ? XD) Look at the post below ;) plz Commented Aug 19, 2016 at 9:07
  • 1
    Of course you can rewrite any (f)xml file programmatically, but this seems like the wrong approach. Just do the changes from java code. If neccessary store data about those changes in some file... Modifying the fxml programmatically is likely much more complicated than just storing the data in a different file... Commented Aug 19, 2016 at 9:17

1 Answer 1

1

Adding New Nodes in FXML

I would say, you should always add new nodes to the FXML unless you have a very good reason not to. Adding nodes to FXML is as easy as adding an extra line to the existing FXML.

<AnchorPane prefHeight="400.0" prefWidth="228.0">
   <children>
      ...
      <Text fx:id="newText"/>
      <TextField fx:id="newTextField"/>
   </children>
</AnchorPane>

These new nodes will be reflected in the scene graph instantaneously and you can bind them to the controller using their fx:id's.

Adding New Nodes in Controller

First, you should have a fx:id defined for the AnchorPane in order to access it in the controller.

<AnchorPane fx:id="anchorPane" prefHeight="400.0" prefWidth="228.0">
   <children>
      ...
   </children>
</AnchorPane>

You can then define the new controls and add them the to AnchorPane in the controller.

public class MyController implements Initializable {

    @FXML
    private AnchorPane anchorPane;
    ...

    public void intialize() {
       ...
       Text newText = new Text("Some Text");
       TextField newTextField = new TextField();
       anchorPane.getChildren().addAll(newText, newTextField);
       ...
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.