Jason Macville
Jason Macville

Reputation: 93

How to dynamically add new Labels below eachother in Javafx

I'm trying to make a leaderboard in my Javafx application. To achieve this, I want to dynamically add Labels containing the name of the player below eachother. I first used a gridpane like this:

Gridpane pane
for(int i = 0; i < users.size(); i++)
{
     Label l = new Label();
     l.setText(users.get(i).getName());
     pane.add(l, 2, 1);
}

(I can't show the whole code, because it is a school project and I'm not allowed to).

But, when I do this, all the names will appear on exactly the same position. Then, I tried to manually change their y locations, but apparently, this changes nothing.

So, what is another good way to dynamically place elements below eachother?

Upvotes: 0

Views: 2506

Answers (1)

zlakad
zlakad

Reputation: 1384

From API docs of GridPane...

Applications may also use convenience methods which combine the steps of setting the constraints and adding the children:

 GridPane gridpane = new GridPane();
 gridpane.add(new Button(), 1, 0); // column=1 row=0
 gridpane.add(new Label(), 2, 0);  // column=2 row=0

Try to understand what would be arguments for your example... (hint - if you are adding all labels to column 2, row 1 what happens?

Upvotes: 2

Related Questions