Search code examples
javajavafxlayoutborderpane

JavaFX BorderPane set multiple buttons to Bottom?


Is it possible to have multiple Buttons set to Bottom (left, center, right)?

This is what I tried:

    private Pane createPane() {
        BorderPane rootPane = new BorderPane();
        rootPane.setTop(createMenueBar());
        rootPane.setCenter(createTableView(model.getIssues()));
        
        rootPane.setBottom(createDeleteIssueButton());  
        rootPane.setBottom(createCloseIssueButton());
        rootPane.setBottom(createCreateNewIssueButton());
        
        BorderPane.setAlignment(deleteIssueButton, Pos.BOTTOM_LEFT);
        BorderPane.setAlignment(closeIssueButton, Pos.BOTTOM_CENTER);
        BorderPane.setAlignment(createIssueButton, Pos.BOTTOM_RIGHT);
        return rootPane;
    }

Result:

enter image description here

As you can see it only shows the last added Button. What is the best way to get this done with JavaFX/BorderPane? I'm very new to this so let me know if you need any more info!


Solution

  • Nested layouts

    Gather the multiple buttons into a layout manager. Place that layout manager object in the bottom position of your BorderPane.

    For example, you might choose FlowPane as your layout manager.

    FlowPane buttons = new FlowPane() ;
    buttons.getChildren().addAll( deleteIssueButton , closeIssueButton , createIssueButton ) ;
    

    The BorderPane places only a single widget in the bottom slot. You want your container of buttons to be that widget.

    BorderPane rootPane = new BorderPane();
    rootPane.setBottom( buttons ) ;
    

    Your use of Pos.BOTTOM_LEFT and such determines where the widget is placed within the bottom slot. The BOTTOM in BOTTOM_LEFT means bottom slot of the given space within a slot, not the bottom of the BorderPane. Two different bottoms involved here.

    BorderPane.setAlignment( buttons , Pos.CENTER ) ;