7 Replies Latest reply on Aug 20, 2013 11:08 PM by 963782

    How to use correctly tool bar?


      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.

        • 1. Re: How to use correctly tool bar?


          • 2. Re: How to use correctly tool bar?

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

            • 3. Re: How to use correctly tool bar?

              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 https://javafx-jira.kenai.com. Reference this forum discussion.

              • 4. Re: How to use correctly tool bar?

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

                • 5. Re: How to use correctly tool bar?

                  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.

                  • 7. Re: How to use correctly tool bar?

                    Thanks, James_D, I've voted for it.