I am trying to create a custom toolbar in javafx. This toolbar should be able to display controls in the center, left and right (three sections) of its surface. The problem is that I have no idea to achieve this. I have read many hints related to this problem, but they do not work for me, or am I doing something wrong ...
In any case, I wrote several methods that represent different ways to implement my toolbar, but none of them work properly. Here you have my attempts:
Using the HBox Hgrow property as spring. Does not work.
public ToolBar createToolBar()
{
ToolBar toolBar = new ToolBar();
Pane emptyPane = new Pane();
HBox spring = new HBox(emptyPane);
spring.setHgrow(emptyPane, Priority.ALWAYS);
toolBar.getItems().addAll(spring, new Label("LABEL"));
return toolBar;
}
2.It works for the left and right parts, but how to determine the center one?
public AnchorPane createToolBar2()
{
AnchorPane toolBar = new AnchorPane();
Label leftLabel = new Label("left");
Label rightLabel = new Label("right");
toolBar.getChildren().addAll(leftLabel, rightLabel);
toolBar.setLeftAnchor(leftLabel, 0.0);
toolBar.setRightAnchor(rightLabel, 0.0);
return toolBar;
}
, , ( StackPane), .
public StackPane createToolBar3()
{
StackPane toolBar = new StackPane();
Button left = new Button("left button");
Button right = new Button("right button");
Button center = new Button("center button");
HBox leftSection = new HBox(left);
leftSection.setAlignment(Pos.CENTER_LEFT);
HBox centerSection = new HBox(center);
centerSection.setAlignment(Pos.CENTER);
HBox rightSection = new HBox(right);
rightSection.setAlignment(Pos.CENTER_RIGHT);
toolBar.getChildren().addAll(leftSection, centerSection, rightSection);
left.setOnAction(event -> System.out.println("left"));
right.setOnAction(event -> System.out.println("right"));
center.setOnAction(event -> System.out.println("center"));
return toolBar;
}
, :
@Override
public void start(Stage stage) {
BorderPane borderPane = new BorderPane();
borderPane.setPrefWidth(500);
borderPane.setPrefHeight(300);
borderPane.setTop(createToolBar4());
stage.setScene(new Scene(borderPane));
stage.show();
}
.