Forum Stats

  • 3,784,037 Users
  • 2,254,874 Discussions


How to use correctly tool bar?

963782 Member Posts: 172
edited Aug 20, 2013 7:08PM in JavaFX 2.0 and Later

I just added items Nodes to tool bar:

toolBar.getItems().addAll(label1, colorPicker1, label2, colorPicker2, label3, colorPicker3, label4, colorPicker4);

when I selected the colorPicker, it can work normally. However, when I specify a preferred width to the tool bar, the overflow button appears and then I selected the colorPicker item in the list of overflow button, nothing responds!

Can somebody explain this phenomenon to me? thanks.


Best Answer


  • 963782
    963782 Member Posts: 172

    I think this is a bug for toolbar, I hope the authorities can correct it in future.

  • James_D
    James_D Member Posts: 1,496 Gold Trophy

    The issue arises because the ToolBar uses a ContextMenu to display the overflow items, with CustomMenuItems wrapping the nodes to be displayed. By default, ContextMenus will hide when you click on a MenuItem contained in the context menu. For a CustomMenuItem, you need to call hideOnClick(false) to prevent this happening (and of course you don't get the chance to do this with the ToolBar's overflow items). You can see the same issue by putting CustomMenuItems wrapping ColorPickers in a ContextMenu as in the following sample code. Uncomment the call to setHideOnClick(false) to see the problem fixed for the context menu.

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.ColorPicker;
    import javafx.scene.control.ContextMenu;
    import javafx.scene.control.CustomMenuItem;
    import javafx.scene.control.Label;
    import javafx.scene.control.ToolBar;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    public class ColorPickerToolBarTest extends Application {
      public void start(Stage primaryStage) {
      ToolBar toolBar = new ToolBar();
      HBox toolbarHolder = new HBox();
      for (int i=0;i<8;i++) {
          toolBar.getItems().add(new ColorPicker());
      VBox root = new VBox();
      Label label= new Label("Right-click here");
      ContextMenu menu = new ContextMenu();
      for (int i=0; i<4; i++) {
          final CustomMenuItem customMenuItem = new CustomMenuItem(new ColorPicker());
    //     customMenuItem.setHideOnClick(false);
      Scene scene = new Scene(root, 600, 200);
      public static void main(String[] args) {

    The same issue arises if you use ComboBoxes instead of ColorPickers.

    I recommend you file a bug report at Reference this forum discussion.

  • 963782
    963782 Member Posts: 172

    Whether we can use the lookup() method to find the CustomMenuItem, then we can use customMenuItem.setHideOnClick(false);  ? Is that possible?

  • James_D
    James_D Member Posts: 1,496 Gold Trophy

    I don't see a way to make that work. CustomMenuItem (and even ContextMenu) does not inherit from Node, so you won't be able to find those directly from a lookup. Moreover, the button for the overflow is a StackPane, which doesn't have a getContextMenu(...) method (the tool bar skin registers a mouse listener to make the context menu pop up when required). So I don't think there's a way to access the custom menu items.

  • 963782
    963782 Member Posts: 172

    Thanks, James_D, I've voted for it.

This discussion has been closed.