Couldosh
Couldosh

Reputation: 391

How to put two buttons in each corner in the bottom of a borderpane

I'm trying to put two different buttons, one at the bottom left corner and another one to the bottom right corner. These two buttons are in differents HBox and the two HBoxes are in the bottom of the Borderpane

To do that I did this, it works but it is ugly:

envoisButton = new Button("Envoyer la commande");
envoisButton.setStyle("-fx-font: 20px \"Roboto\";");
envoisButton.setPrefSize(300,50);
envoisButton.setGraphic(new ImageView(new Image("PleinVide/images/OK.png")));
HBox.setMargin(envoisButton,new Insets(0,20,20,20));
HBox rightButtons = new HBox(envoisButton);
rightButtons.setAlignment(Pos.CENTER_RIGHT);

synchroButton = new Button("Synchroniser");
synchroButton.setStyle("-fx-font: 20px \"Roboto\";");
synchroButton.setPrefSize(300,50);
synchroButton.setGraphic(new ImageView(new Image("PleinVide/images/synchronize.png")));
HBox.setMargin(synchroButton,new Insets(0,20,20,20));
HBox leftButtons = new HBox(synchroButton);
leftButtons.setAlignment(Pos.CENTER_LEFT);
HBox buttons = new HBox(leftButtons,rightButtons);
buttons.setSpacing(primaryScreenBounds.getWidth()*0.65); //This is very ugly (primaryScreenBounds == my screen resolution)

borderPane.setBottom(buttons);

Here's the result: Expected buttons positions We can see that the 2 buttons are where I want them to be, but if in the future I want to add another button, this will not work at all :/

Do you have a solution to place the two buttons at the corners? Thank you very much.

Upvotes: 1

Views: 3148

Answers (3)

Slaw
Slaw

Reputation: 46170

Wrap the two HBoxes in another HBox, set the appropriate alignments, and have them both always grow horizontally.

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;

public class App extends Application {

  @Override
  public void start(Stage primaryStage) throws Exception {
    BorderPane root = new BorderPane(new Label("Hello, World!"));

    HBox leftBox = new HBox(new Button("Left Button"));
    leftBox.setAlignment(Pos.CENTER_LEFT);
    HBox.setHgrow(leftBox, Priority.ALWAYS);

    HBox rightBox = new HBox(new Button("Right Button"));
    rightBox.setAlignment(Pos.CENTER_RIGHT);
    HBox.setHgrow(rightBox, Priority.ALWAYS);

    HBox bottom = new HBox(leftBox, rightBox);
    bottom.setPadding(new Insets(10));
    root.setBottom(bottom);

    primaryStage.setScene(new Scene(root, 600, 400));
    primaryStage.show();
  }

}

Another option is to use a single HBox and use a "filler" region/pane. For example:

HBox hbox = new HBox();
hbox.getChildren().add(new Button("Left Button"));

Pane filler = new Pane();
hbox.getChildren().add(filler);
HBox.setHgrow(filler, Priority.ALWAYS);

hbox.getChildren().add(new Button("Right Button"));

borderPane.setBottom(hbox);

Upvotes: 6

Przemek Krysztofiak
Przemek Krysztofiak

Reputation: 924

I would suggest

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>


<BorderPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <center>
      <Pane style="-fx-background-color: red;" BorderPane.alignment="CENTER" />
   </center>
   <bottom>
      <AnchorPane maxHeight="-Infinity" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" style="-fx-background-color: green;" BorderPane.alignment="CENTER">
         <children>
            <Button text="Button" AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="20.0" AnchorPane.topAnchor="20.0" />
            <Button text="Button" AnchorPane.bottomAnchor="20.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="20.0" />
         </children>
      </AnchorPane>
   </bottom>
</BorderPane>

Upvotes: 0

EverNight
EverNight

Reputation: 1064

you could use a gridpane.

Either instead of adding buttons in your example, add a gridpane, and add the buttons in that gridpane.

Or

Use a gridpane as your scene and style and add constraints accordingly (this is more difficult). This way if you want to add buttons, all you need to do is figure out the coordinates for the slots in the gridpane.

Upvotes: -1

Related Questions