This discussion is archived
7 Replies Latest reply: Aug 20, 2013 4:08 PM by 963782 RSS

How to use correctly tool bar?

963782 Newbie
Currently Being Moderated

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?
    963782 Newbie
    Currently Being Moderated

    Up...

  • 2. Re: How to use correctly tool bar?
    963782 Newbie
    Currently Being Moderated

    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?
    James_D Guru
    Currently Being Moderated

    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 {
     @Override
      public void start(Stage primaryStage) {
      ToolBar toolBar = new ToolBar();
      toolBar.setPrefWidth(400);
      HBox toolbarHolder = new HBox();
      toolbarHolder.getChildren().add(toolBar);
      for (int i=0;i<8;i++) {
          toolBar.getItems().add(new ColorPicker());
      }
      VBox root = new VBox();
      root.getChildren().add(toolbarHolder);
      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);
                menu.getItems().add(customMenuItem);
      }
      label.setContextMenu(menu);
      root.getChildren().add(label);
      Scene scene = new Scene(root, 600, 200);
      primaryStage.setScene(scene);
      primaryStage.show();
      }
      public static void main(String[] args) {
      launch(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?
    963782 Newbie
    Currently Being Moderated

    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?
    James_D Guru
    Currently Being Moderated

    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?
    963782 Newbie
    Currently Being Moderated

    Thanks, James_D, I've voted for it.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points