2

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!

0xh3xa
  • 4,801
  • 2
  • 14
  • 28
Chris
  • 1,828
  • 6
  • 40
  • 108

1 Answers1

3

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 ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • how would I do that ? Never used a layout manager before – Chris Jul 11 '20 at 01:16
  • @Chris Yes, you did use a layout manager before. The `BorderPane` is a layout manager. – Basil Bourque Jul 11 '20 at 01:18
  • so I create another `Pane` , then add the buttons to `left`, `center` & `right` and then add the `Pane` to the bottom to `rootPane`? – Chris Jul 11 '20 at 01:21
  • @Chris Yes. You can nest various layout managers within other layout managers. If you think creatively, you can create nice forms this way. But don't go crazy, minimize your nested layouts where you can. But do nest them where needed to get an effective layout. – Basil Bourque Jul 11 '20 at 01:24
  • that worked thanks! one more thing, what would I have to do if I wanted to have the first two button allign left, next to each other, and the last button on the right ? – Chris Jul 11 '20 at 01:24
  • @Chris And the fun begins… You need to practice with all the various layout managers, to get familiar. Then you will be able arrange your widgets in more sophisticated layouts as you ask. – Basil Bourque Jul 11 '20 at 01:26
  • 2
    @Chris Read some tutorials on JavaFX/OpenJFX layout managers. I have seen some good ones. Visual screenshots will bring home the concepts for you. And you may find similar tutorials for [Swing](https://en.wikipedia.org/wiki/Swing_(Java)) — you can skim through these too to glean ideas as some of the layout managers are quite similar in concept ( `BorderPane` in JavaFX is a copy of `BorderLayout` in Swing), but ignore the Swing code details. – Basil Bourque Jul 11 '20 at 01:28