1

I want to create an Array of JavaFX controllers in order to be easier to work with them e.g. you could loop adding/setting elements in a GridPane.

But despite of the compiler/IDE not displaying any error this code below doesn't work:

public GridPane drawPane(){
    GridPane grid = new GridPane();
    Button[] btn = new Button[10];
    grid.add(btn[0], 0,0);
    return grid;
}

however this one does work:

public GridPane drawPane(){
    GridPane grid = new GridPane();
    Button btn = new Button();
    grid.add(btn, 0,0);
    return grid;
}

Am I instancing the controllers wrong? Why this code doesn't work with arrays ?

3
  • Where do you fill your arrays with Controls in the first example? Commented May 7, 2014 at 6:57
  • I tried it with a for loop, but I am trying to point out that even with a single instance of that array it doesn't works. Commented May 7, 2014 at 7:23
  • 1
    Of course your first example does not work. btn[0] is null. Initialize it properly and it will work. Commented May 7, 2014 at 8:32

2 Answers 2

5

Try this... It will create an Array of Buttons and if you call your getGrid() Method it iterates through this Array of Buttons and adds them to the GridPane.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class App extends Application {

    private Button[] btns = new Button[10];

    @Override
    public void start(Stage primaryStage) {

        initBtnsArray();
        Group root = new Group();

        root.getChildren().add(getGrid());
        Scene scene = new Scene(root, 800, 600);

        primaryStage.setTitle("Hello Controller-Array-World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    private Pane getGrid() {
        int i = 0;
        GridPane gridPane = new GridPane();
        for(Button b : btns) {
            // do something with your button
            // maybe add an EventListener or something
            gridPane.add(b, i*(i+(int)b.getWidth()), 0);
            i++;
        }
        return gridPane;
    }

    private void initBtnsArray() {
        for(int i = 0; i < btns.length; i++) {
            btns[i] = new Button("Button-"+i);
        }
    }
}

Patrick

Sign up to request clarification or add additional context in comments.

Comments

5

Your Array does not contain any Objects, so you will get a NullPointerException. Fill the array with an initialized Object to get it working.

public GridPane drawPane(){
    GridPane grid = new GridPane();
    Button[] btn = new Button[10];
    btn[0] = new Button(); //add this line
    grid.add(btn[0], 0,0);
    return grid;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.