0

I'm new to and trying to learning JavaFX and FXML. Most of my application logic is in the FXMLController class and the base class is pretty much empty except the basic code that was generated by NetBeans IDE such as below

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();
}

I have an element with ID input1 that is of type TextField. How can i access this (or any other) control by its ID? (keeping in mind that I am in the controller class and not the main class).

I found this question below which is exactly what I'm looking for but that situation is different because they are in the main class where scene is defined. How can i access scene from the controller class and use the code in the question below.

How to find an element with an ID in JavaFX?

Community
  • 1
  • 1

1 Answers1

1

This is my first time answering a question on Stack Overflow so please go easy on me.

I am new to JavaFX as well and I too had a problem with this. This is what if found. Your TextField in your FXMLDocument.fxml must have a fx:id assigned to it, as in:

<TextField fx:id="input1" layoutX="0.5" layoutX="0.5" />

If you are using the JavaFX SceneBuilder then you can find the fx:id under "Code: TextField" on the right side.

Then in your controller class you can access it but using.

@FXML public TextField input1;

You can use an ArrayList to loop through all of your TextFields. Here is an example.

@FXML public TextField input1;
@FXML public TextField input2;
@FXML public TextField input3;
@FXML public TextField input4;
@FXML public TextField input5;
@FXML public TextField input6;
@FXML public TextField input7;
@FXML public Button button;

List<TextField> inputs = new ArrayList<TextField>();

public void displayText(ActionEvent event) {
    inputs.add(input1);
    inputs.add(input2);
    inputs.add(input3);
    inputs.add(input4);
    inputs.add(input5);
    inputs.add(input6);
    inputs.add(input7);
    for (int x = 0; x < 7; x++) {
        System.out.println(inputs.get(x).getText());
    }
}

There might be a simpler way, but this way works for me.

Givver
  • 423
  • 4
  • 7
  • You can call lookup(...) or lookupAll(...) on anything in the scene graph that is an ancestor of the nodes you're looking for. So just use injection (fx:id and @FXML annotation) to get a reference to something that contains all your text fields (for example the root element), and call lookup(...) on that. Lookups are a bit fragile; they are only guaranteed to work once the scene has been rendered (so won't work in the initialize(...) method. You might be better just putting the text fields in a list or array and iterating through it. – James_D Dec 21 '13 at 18:01
  • An ArrayList is probably your best bet. I will update my answer to show an example. – Givver Dec 21 '13 at 18:49
  • Thanks for the help. I'll use List for now, seems good enough for a simple project I'm working in. –  Dec 21 '13 at 19:01