1

I am trying to create array of TextFields in JavaFX but getting error. I have 10 TextFields: path1, path2... path10.

//initialization
@FXML
private TextField path1,path2,path3,path4,path5,path6,path7,path8,path9,path10;
@FXML
private TextField[] paths = {path1,path2,path3,path4,path5,path6,path7,path8,path9,path10};

However, when I write

String text = paths[0].getText();
paths[1].setText(name);

This first line gets me NullPointerException.

The solution I found is to use Initizlizatior of my Сontroller, but this is so ugly:

public void initialize(URL fxmlFileColation, ResourceBundle resources) {
    paths[0] = path1;
    paths[1] = path2;
    paths[2] = path3;
    paths[3] = path4;
    paths[4] = path5;
    paths[5] = path6;
    paths[6] = path7;
    paths[7] = path8;
    paths[8] = path9;
    paths[9] = path10;
}

How can I get rid of manual assinment in many lines and make

@FXML
 private TextField[] paths = {path1,path2,path3,path4,path5,path6,path7,path8,path9,path10};

work?

Vittori0
  • 117
  • 3
  • 10
  • Possible duplicate of [create array of Label using FXML in JavaFX](http://stackoverflow.com/questions/28587297/create-array-of-label-using-fxml-in-javafx) – DVarga Jul 07 '16 at 10:20

3 Answers3

2

You cannot make this exact code work. The array creation in

private TextField[] paths = {path1,path2,path3,path4,path5,path6,path7,path8,path9,path10};

happens in the initializer. The loading process for fxml files works like this however:

  1. Create controller class instance (or use a existing one).
  2. Load fxml content injecting objects created by the loader.
  3. Call initialize on the controller, if it exists.

The above code is executed in step 1 while the objects you want to store in the array are created later in step 2.

You can work around this by using the fxml to create a List of the TextFields and inject it to the controller (see my answer to Grouping together JavaFX FXML Objects)

Of course you can also create a array of TextFields containing specific elements using a array initializer in the initialize method:

public void initialize() {
    paths = new TextField[] {path1,path2,path3,path4,path5,path6,path7,path8,path9,path10};
}
Community
  • 1
  • 1
fabian
  • 80,457
  • 12
  • 86
  • 114
  • Thank you for detailed answer! However I cannot understand first part of your answer. Just made it work in Void Initialise(). Could you please suggest reading to understand this? – Vittori0 Jul 07 '16 at 13:04
  • @Vittori0: I described the approach in the answer I linked. It just inserts the `Node`s to the scene graph and a list. See [*Introduction to FXML*](https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html), sections "Define Blocks"/fx:reference. If you were refering to the description of the loading process, read the Controllers section on that page;fields that are initialized during the creation of the object, which happens before the `FXMLLoader` uses the controller instance later by injecting the fields and this is when the `path` fields become non-`null`. – fabian Jul 07 '16 at 13:53
0

I don't think you can. You need to declare the variables separately so FXML can initialise the variable. Then, you would need to make your TextField array work by setting each element to something. The best way of doing it would be to use the solution you have in the initialize(...) function and use it for what it meant to do – initialising things.

ifly6
  • 5,003
  • 2
  • 24
  • 47
0

A simple solution i use

 TextField[] txt = new TextField[beanFields.length];
 for (int i=0; i<=beanFields.length-1; i++) {
                TextField textField = new TextField();
                txt[i] = textField;
                textField.setPrefWidth(200);
                textField.setPrefHeight(32);
    }
Howard J
  • 421
  • 3
  • 7