Reputation: 111
I want to change the location of each buttom in the hbox:
HBox buttom = new HBox();
Button delete_button = new Button("Delete");
Button showAll_button = new Button("Show All");
Button back_button = new Button("Back");
buttomPaneRight_deleteScene.getChildren().addAll(back_button,showAll_button,delete_button);
BorderPane basePane = new BorderPane();
basePane.setButtom(buttomPaneRight_deleteScene):
I want to change each button location for example back_button
to be in the left corner and delete_button
, showAll_button
to be in the right corner.
I checked setAlignment(Pos.Value);
but this change the whole hbox pos
Upvotes: 1
Views: 10229
Reputation: 209319
One solution (there are many) is simply to wrap the buttons you want in the right in another HBox
:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class LayoutExample extends Application {
@Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
HBox hbox = new HBox();
Button backButton = new Button("Back");
Button deleteButton = new Button("Delete");
Button showAllButton = new Button("Show All");
HBox rightButtons = new HBox(deleteButton, showAllButton);
rightButtons.setAlignment(Pos.CENTER_RIGHT);
HBox.setHgrow(rightButtons, Priority.ALWAYS);
hbox.getChildren().addAll(backButton, rightButtons);
hbox.setPadding(new Insets(2));
root.setBottom(hbox);
Scene scene = new Scene(root, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Another solution is to add a Pane
that acts as a spacer, and make it grow as much as it can:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class LayoutExample extends Application {
@Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
HBox hbox = new HBox();
Button backButton = new Button("Back");
Button deleteButton = new Button("Delete");
Button showAllButton = new Button("Show All");
Pane spacer = new Pane();
HBox.setHgrow(spacer, Priority.ALWAYS);
hbox.getChildren().addAll(backButton, spacer, deleteButton, showAllButton);
hbox.setPadding(new Insets(2));
root.setBottom(hbox);
Scene scene = new Scene(root, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
A third solution is to use an AnchorPane
instead of a HBox
, and wrap the two buttons on the right in a HBox
:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class LayoutExample extends Application {
@Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
AnchorPane anchorPane = new AnchorPane();
Button backButton = new Button("Back");
Button deleteButton = new Button("Delete");
Button showAllButton = new Button("Show All");
HBox rightButtons = new HBox(deleteButton, showAllButton);
anchorPane.getChildren().addAll(backButton, rightButtons);
AnchorPane.setBottomAnchor(rightButtons, 2.0);
AnchorPane.setBottomAnchor(backButton, 2.0);
AnchorPane.setLeftAnchor(backButton, 2.0);
AnchorPane.setRightAnchor(rightButtons, 2.0);
root.setBottom(anchorPane);
Scene scene = new Scene(root, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
And a fourth solution is to use a GridPane
:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class LayoutExample extends Application {
@Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
GridPane gridPane = new GridPane();
Button backButton = new Button("Back");
Button deleteButton = new Button("Delete");
Button showAllButton = new Button("Show All");
gridPane.add(backButton, 0, 0);
gridPane.add(deleteButton, 1, 0);
gridPane.add(showAllButton, 2, 0);
ColumnConstraints leftCol = new ColumnConstraints();
leftCol.setHgrow(Priority.ALWAYS);
gridPane.getColumnConstraints().addAll(leftCol, new ColumnConstraints(), new ColumnConstraints());
gridPane.setPadding(new Insets(2));
root.setBottom(gridPane);
Scene scene = new Scene(root, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Upvotes: 3
Reputation: 125
Try using a different Pane option, as I mentioned before, a GridPane should solve your issue, that or nesting another HBox
See this http://docs.oracle.com/javafx/2/layout/builtin_layouts.htm
Upvotes: 2
Reputation: 44110
Basically don't use a HBox
. An AnchorPane
is more suited to what you want to achieve.
AnchorPane holder = new AnchorPane();
Button delete_button = new Button("Delete");
Button showAll_button = new Button("Show All");
Button back_button = new Button("Back");
// Add buttons to holder
holder.getChildren().add(delete_button);
holder.getChildren().add(showAll_button);
holder.getChildren().add(back_button);
// Delete top left
AnchorPane.setTopAnchor (delete_button, 0.0);
AnchorPane.setLeftAnchor(delete_button, 0.0);
// Show all top right
AnchorPane.setTopAnchor (showAll_button, 0.0);
AnchorPane.setRightAnchor(showAll_button, 0.0);
// Back bottom right
AnchorPane.setBottomAnchor(back_button, 0.0);
AnchorPane.setRightAnchor (back_button, 0.0);
Upvotes: 1