If one of the ToolBar items has a drop-down itself, such as a MenuButton, the ToolBar dropdown window becomes unusable, as it gets dismissed as soon as you click on the MenuButton and release the mouse, meaning before you can get to select a MenuItem from it.
In the sample below, resize the application window such that the ToolBar items no longer fit, click to open the ToolBar drop-down window, and try to select an item from MenuButton.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class ToolBarDropDown extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Button btn = new Button();
btn.setText("Button");
MenuButton menuBtn = new MenuButton();
menuBtn.setText("MenuButton");
MenuItem menuItem = new MenuItem("MenuItem");
menuBtn.getItems().setAll(menuItem);
ToolBar toolbar = new ToolBar();
toolbar.getItems().setAll(btn, menuBtn);
VBox root = new VBox();
root.getChildren().setAll(toolbar);
Scene scene = new Scene(root, 300, 100);
primaryStage.setScene(scene);
primaryStage.initStyle(StageStyle.UTILITY);
primaryStage.show();
}
}
In the sample below, resize the application window such that the ToolBar items no longer fit, click to open the ToolBar drop-down window, and try to select an item from MenuButton.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class ToolBarDropDown extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Button btn = new Button();
btn.setText("Button");
MenuButton menuBtn = new MenuButton();
menuBtn.setText("MenuButton");
MenuItem menuItem = new MenuItem("MenuItem");
menuBtn.getItems().setAll(menuItem);
ToolBar toolbar = new ToolBar();
toolbar.getItems().setAll(btn, menuBtn);
VBox root = new VBox();
root.getChildren().setAll(toolbar);
Scene scene = new Scene(root, 300, 100);
primaryStage.setScene(scene);
primaryStage.initStyle(StageStyle.UTILITY);
primaryStage.show();
}
}