Search code examples
user-interfacejavafxgridpane

How can I create a GridPane with 2 rows and 3 columns programatically in JavaFx?


I am trying to create a grid pane with 2 rows and 3 columns and I want to insert some text in each part programatically . Here is what I've done but I keep getting errors .

GridPane gp = new GridPane();
        RowConstraints row0 = new RowConstraints();
        RowConstraints row1 = new RowConstraints();
        gp.getRowConstraints().addAll(row0 , row1);
        ColumnConstraints col0 = new ColumnConstraints();
        ColumnConstraints col1 = new ColumnConstraints();
        ColumnConstraints col2 = new ColumnConstraints();
        gp.getColumnConstraints().addAll(col0 , col1,col2);
        Text txt = new Text("text");
        gp.add(txt, 0, 0);
        gp.add(txt, 1, 0);
        gp.add(txt, 2, 0);
        gp.add(txt, 0, 1);
        gp.add(txt, 1, 1);
        gp.add(txt, 2, 1);

Hourse has past but I cant figure where my mistake is ? :|


Solution

  • I believe the issue is that you are using the same Text several times over in the GridPane. I encountered a similar issue when adding the same image multiple times to a GridPane. You just need to create multiple Texts - one for each cell.