TextField and Label Array

Here I'm trying to create TextField and Label in arrays and add them to a GridPane but getting problem in commented line and onwards.

GridPane pane=new GridPane();
pane.setPadding(new Insets(50,0,0,50));
pane.setVgap(20);
pane.setHgap(40);
Label Arrival= new Label("Arrival Time");
Label Burst= new Label("Burst Time");
TextField[] ArrvialInput= new TextField[10];
TextField[] BurstInput= new TextField[10];
Label Process= new Label();
for (int i=0; i<=10-1; i++) {
    TextField textFieldA = new TextField();
    TextField textFieldB = new TextField();
    ArrvialInput[i] = textFieldA;
    BurstInput[i] = textFieldB;
    Process= new Label("P"+(i+1));
}
for (int i=0; i<=10-1; i++) {
    pane.add(Process, 0, i+1); //Run Time error here 
    pane.add(Arrival, 1, i+1);
    pane.add(ArrvialInput[i], 2, i+1);
    pane.add(Burst, 0, i);
    pane.add(BurstInput[i], 4, i+1);
}

1 answer

  • answered 2018-10-23 14:45 Matt

    You can do something like this if you don't need to access the labels but @fabian is correct in saying "Adding the same node to the same parent multiple times results in an exception"

    public class Main extends Application {
    
        @Override
        public void start(Stage stage) {
            GridPane pane=new GridPane();
            pane.setPadding(new Insets(50,0,0,50));
            pane.setVgap(20);
            pane.setHgap(40);
    //        Label Arrival= new Label("Arrival Time");
    //        Label Burst= new Label("Burst Time");
            TextField[] ArrvialInput= new TextField[10];
            TextField[] BurstInput= new TextField[10];
    //        Label Process= new Label();
            for (int i=0; i<=10-1; i++) {
                TextField textFieldA = new TextField();
                TextField textFieldB = new TextField();
                ArrvialInput[i] = textFieldA;
                BurstInput[i] = textFieldB;
    //            Process= new Label("P"+(i+1));
            }
            for (int i=0; i<=10-1; i++) {
                pane.add(new Label("P"+(i+1)), 0, i+1);
                pane.add(new Label("Arrival Time"), 1, i+1);
                pane.add(ArrvialInput[i], 2, i+1);
                pane.add(new Label("Burst Time"), 3, i+1);
                pane.add(BurstInput[i], 4, i+1);
            }
    
    
            Scene scene = new Scene(pane);
            stage = new Stage();
            stage.setScene(scene);
            stage.show();
    
        }
    
    
        public static void main(String[] args) { launch(args); }
    }