Marco Jakob Marco Jakob - 1 year ago 105
Java Question

How to set multiple buttons and add them to a gridPane

for a GUI i have to set multiple buttons to a

. For this i want to ask if there is a way to add multiple buttons in a Grid in a more elegant way than i do.
Here is an extractof my Code.

Button button0 =new Button("0");
Button button1 =new Button("1");
Button button2 =new Button("2");
Button button3 =new Button("3");
Button button4 =new Button("4");
Button button5 =new Button("5");
Button button6 =new Button("6");
Button button7 =new Button("7");
Button button8 =new Button("8");
Button button9 =new Button("9");
Button cancel = new Button("C");
Button plus = new Button("+");
Button minus = new Button("-");
Button multiplicate = new Button("*");
Button divide = new Button("/");
Button equal = new Button("=");

For all of those buttons i have to set the row and column index separately.

root.setPadding(new Insets(5,5,5,5));
GridPane.setConstraints(root, 4, 3);
root.add(label, 1, 1, 3, 1);
root.add(button1, 2, 2);
root.add(button2, 3, 2);
root.add(button3, 4, 2);
root.add(button4, 2, 3);
root.add(button5, 3, 3);
root.add(button6, 4, 3);
root.add(button7, 2, 4);
root.add(button8, 3, 4);
root.add(button9, 4, 4);
root.add(button0, 2, 5);
root.add(cancel, 3, 5);
root.add(plus, 5,2);
root.add(minus, 5,3);
root.add(multiplicate, 5, 4);
root.add(divide, 5,5);
root.add(equal, 4, 5);

I would be very greateful for any suggestions.

Answer Source

You shall use an FXML file as was suggested in a comment also.

But if you want to stick with Java code, you can do something like this (my suggestion is still have an FXML file):

public class Main extends Application {
    public void start(Stage primaryStage) {
        try {
            GridPane root = new GridPane();
            Scene scene = new Scene(root, 400, 400);

            Button b = new Button();
            b.setOnAction(new EventHandler<ActionEvent>() {

                public void handle(ActionEvent event) {

            GridHelper.addToGrid(root, new Button("0"), 2, 5);
            GridHelper.addToGrid(root, new Button("1"), 2, 2);
            GridHelper.addToGrid(root, new Button("2"), 3, 2);
            GridHelper.addToGrid(root, new Button("3"), 4, 2);
            GridHelper.addToGrid(root, new Button("4"), 2, 3);
            GridHelper.addToGrid(root, new Button("5"), 3, 3);
            GridHelper.addToGrid(root, new Button("6"), 4, 3);
            GridHelper.addToGrid(root, new Button("7"), 2, 4);
            GridHelper.addToGrid(root, new Button("8"), 3, 4);
            GridHelper.addToGrid(root, new Button("9"), 4, 4);

            GridHelper.addToGrid(root, new Button("C"), 3, 5, e -> System.out.println("C"));
            GridHelper.addToGrid(root, new Button("+"), 5, 2, e -> System.out.println("+"));
            GridHelper.addToGrid(root, new Button("-"), 5, 3);
            GridHelper.addToGrid(root, new Button("*"), 5, 4);
            GridHelper.addToGrid(root, new Button("/"), 5, 5);
            GridHelper.addToGrid(root, new Button("="), 4, 5);

            root.setPadding(new Insets(5, 5, 5, 5));


        } catch (Exception e) {

    static class GridHelper {

        static void addToGrid(GridPane grid, Node c, int col, int r) {
            grid.add(c, col, r);

        static void addToGrid(GridPane grid, Node c, int col, int r, EventHandler<ActionEvent> event) {
            grid.add(c, col, r);
            addButtonEvent(c, event);

        static void addToGrid(GridPane grid, Node c, int col, int r, int cs, int rs) {
            grid.add(c, col, r, cs, rs);

        static void addToGrid(GridPane grid, Node c, int col, int r, int cs, int rs, EventHandler<ActionEvent> event) {
            grid.add(c, col, r, cs, rs);
            addButtonEvent(c, event);

        static void addButtonEvent(Node button, EventHandler<ActionEvent> event) {
            if (button instanceof Button)
                ((Button) button).setOnAction(event);


    public static void main(String[] args) {

It just simply introduces a static class that exposes some static methods to add a Node to a GridPane to the specified position, and optionally set the spans (row/colum). If it is a Button you can add a press event also. Could be generalized further of course, it is just a sample for the exact question.

Also you could make a loop for Buttons 0 - 9 (as it was proposed in the comment).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download