Search code examples
javafxgridpane

How can labels be iteratively added to a GridPane in JavaFX?


I'm trying to iteratively add labels to a GridPane in JavaFX using a for loop, but I keep on getting an error when I try to launch the application. The console output is:

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=10.0, vgap=10.0, alignment=TOP_CENTER
    at javafx.scene.Parent$2.onProposedChange(Parent.java:454)
    at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
    at javafx.scene.layout.GridPane.add(GridPane.java:965)
    at com.comsci.drt.ProjectGUI.start(ProjectGUI.java:181)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$5(GtkApplication.java:139)

I'm a beginner in this, but I understand that it's telling me I'm trying to place an object which has already been placed.

Code:

Label l = new Label();
for(int i = 2; i <= 3; i++) {
    l.setText(finalResultsArray[i].getTeamName());   <-- line 181
    pane1.add(l, 0, i);
}

I then proceeded to use an array of labels which I believe wouldn't be trying to replace an object which is already placed, but I receive another error:

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at com.comsci.drt.ProjectGUI.start(ProjectGUI.java:175)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$5(GtkApplication.java:139)

Code:

Label[] label = new Label[12];
for(int i = 0; i < 12; i++) {
    for(int j = 0; j < 1; j++) {
        label[i].setText(finalResultsArray[i].getTeamName());
        pane1.add(label[i], 0, i);
    }
}

Solution

  • You need to use a new Label instance every time you want to add text to the grid. Furthermore newly created arrays are filled with null values.

    Possible ways to make this work therefore are

    for(int i = 2; i <= 3; i++) {
        Label l = new Label();
        l.setText(finalResultsArray[i].getTeamName());
        pane1.add(l, 0, i);
    }
    

    or

    Label[] label = new Label[12];
    for(int i = 0; i < 12; i++) {
        label[i] = new Label();
        label[i].setText(finalResultsArray[i].getTeamName());
        pane1.add(label[i], 0, i);
    }