Klotired
Klotired

Reputation: 43

JavaFX - How do i create a button for each element on my array?

So i want to create a list of button for each element on my array. If i have 10 objects on the array, i want to create 10 buttons right away. Thank you

Upvotes: 0

Views: 1085

Answers (1)

User
User

Reputation: 303

Create buttons where? You need a container to add them to (IIRC something that extends Parent).

String[] sa = new String[10];
for (String s : sa) {
  Button b = new Button(s);
  vBox.getChildren().add(b);
}

If you use SceneBuilder and FXML to design your views (leaving out anything dynamic like this), you can assign containers like a VBox an ID, which will automatically add a reference with an @FXML annotation when you generate a controller class. That will give you a reference to a container to add the buttons to.

Upvotes: 1

Related Questions