How can I make a for loop that will display 8 TextFields in javaFx

T SoVz :

This is currently the code that I have so far...

int nums = 8;
for (int j = 0; j < nums; j++) {
   TextField test = new TextField();
   test.setAlignment(Pos.CENTER);
}
this.getChildren().add(test);

I have tried doing somehting like TextField 'test' + j = new TextField(); so that it would create test1, test2, test3 ect. but that gave syntax errors. Not really sure on how I would go about this in any other way.

Dustin R :

Add them to something that allows them to stack vertically. Also, make sure your call to add them is inside the loop where test is still in scope.

VBox box = new VBox(5);
int nums = 8;
for (int j = 0; j < nums; j++) {
   TextField test = new TextField();
   test.setAlignment(Pos.CENTER);
   box.getChildren().add(test);
}
this.getChildren().add(box);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=23203&siteId=1
Recommended