user5988819
user5988819

Reputation:

JavaFX: How to create two columns of buttons?

I'm trying to create an elevator button pad simulator with buttons labeled 1-10 with 5 rows and two columns. Here is an example of what I'm trying to explain:

9        10
7         8
5         6
3         4
1         2

Currently, my code has 10 buttons but all merged into one column which I do not want. And looks something like this:

1
1
2
2
3
3
4
4
5
5

And here is just a snippet of my code: (don't mind the commented parts)

 @Override
public void start(Stage primaryStage) {
    // Get the pane for the scene
    primaryStage.setScene(new Scene(getPane(), 180, 600));

    // Setup the stage
    primaryStage.setTitle("Elevator Buttons");
    primaryStage.setResizable(false);
    primaryStage.show();
}

protected Pane getPane() {
    Pane pane = new VBox(10);
    pane.setPadding(new Insets(40));
   GridPane grid = new GridPane();
    //for (int i = row - 1; i >= 0; i-=2) {
       // for (int k = col - 1; k >= 0; i-=1) {

    for (int i = 0; i < row; i++) {
        for(int k =0; k <col; k++) {
            // Set the button number as text for the button
            buttonsArray[i][k] = new Button(Integer.toString(i + 1));

            // Set preferred width and style with a light gray background
            buttonsArray[i][k].setPrefWidth(100);
            buttonsArray[i][k].setStyle("-fx-font: 22 arial; -fx-base: LightGray");

            // Add the button to the pane and set the handler
            pane.getChildren().add(buttonsArray[i][k]);
            buttonsArray[i][k].setOnAction(ButtonHandler);
        }
    }
    return pane;
}

So how would I make two columns of buttons with an array?

Upvotes: 0

Views: 1649

Answers (2)

DVarga
DVarga

Reputation: 21799

Exchange this:

pane.getChildren().add(buttonsArray[i][k]);

with this:

grid.add(buttonsArray[i][k],k,i);

That's what James_D proposes in his comment.

Upvotes: 0

Uwe
Uwe

Reputation: 844

As pointed out in the comments and other answer(s), you can use a GridPanefor what you want to achieve. This code should do the job, including the correct labelling of the elevator buttons:

@Override
    public void start(Stage primaryStage) {
        // Get the pane for the scene
        primaryStage.setScene(new Scene(getPane(), 180, 600));

        // Setup the stage
        primaryStage.setTitle("Elevator Buttons");
        primaryStage.setResizable(false);
        primaryStage.show();
    }

    protected Pane getPane() {
        Pane pane = new VBox(10);
        pane.setPadding(new Insets(40));
        GridPane grid = new GridPane();

        for (int i = 0; i < row; i++) {
            for (int k = 0; k < col; k++) {
                // Set the button number as text for the button
                Button button = new Button(Integer.toString((row * col - 2 * i - 1) + k));

                button.setPrefWidth(100);
                button.setStyle("-fx-font: 22 arial; -fx-base: LightGray");

                // Add the button to the pane and set the handler
                grid.add(button, k, i);
                button.setOnAction(buttonHandler);
            }
        }
        return grid;
    }

Upvotes: 1

Related Questions