Forum Stats

  • 3,854,249 Users
  • 2,264,344 Discussions
  • 7,905,621 Comments

Discussions

JavaFX menu button

awacs5
awacs5 Member Posts: 17
edited Mar 28, 2017 8:45PM in JavaFX 2.0 and Later

Hi,

I'm developing an application toolbar and I would like to change the click behaviour of the menu button. So basically I'd like the menu button to open when the user clicks on it and stay open until the user clicks on it again (second click on it will close it).

So I started off by adding a mouse click eventfilter to the menu button. This all seems to work good, but when I click somewhere else on the screen (not on the menu) the menu will auto close. The mouse event filter is not triggered when I click somewhere else. How can I keep the menu open until the user clicks on it a second time?

Thanks

Answers

  • jsmith
    jsmith Member Posts: 2,856
    edited Mar 28, 2017 3:47PM

    The popup for the MenuButton is implemented as a ContextMenu.

    You can extend MenuButtonSkin, and set the popup's autoHide property to false.

    Then set your MenuButton to use your custom skin.

    Note: MenuButtonSkin is a com.sun class, so extending it works in Java 8, I'm not sure if it will continue to work in Java 9+.

    See the javadoc for the autoHide property.

    Specifies whether Popups should auto hide. If a popup loses focus and autoHide is true, then the popup will be hidden automatically.

    Note that if the window containing the context menu loses focuses, the popup will be hidden regardless of the autohide setting.  So the setting just deals with clicks within the window; when autohide is false and you click within the window and the context menu is showing, then the context menu will not be hidden (until you click on the context menu to make a choice from it or you click on the menu button itself).

    import com.sun.javafx.scene.control.skin.MenuButtonSkin;
    import javafx.application.Application;
    import javafx.geometry.Insets;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;

    public class PersistentMenuButton extends Application {

       @Override public void start(Stage stage) {

      MenuButton menuButton = new MenuButton("Alice");
       menuButton.getItems().setAll(

       new MenuItem("Not quite"),
      new MenuItem("Almost")

      );

       menuButton.setSkin(new PersistentMenuButtonSkin(menuButton));

       StackPane layout = new StackPane(menuButton);
       layout.setPadding(new Insets(100));

       stage.setScene(new Scene(layout));
       stage.show();
       }

       public static void main(String[] args) {

       launch(args);
       }

    }

    class PersistentMenuButtonSkin extends MenuButtonSkin {

       PersistentMenuButtonSkin(MenuButton menuButton) {

       super(menuButton);
       popup.setAutoHide(false);
       }

    }

  • bouye-JavaNet
    bouye-JavaNet Member Posts: 394 Silver Badge
    edited Mar 28, 2017 8:45PM

    The skins were supposed to become public in JDK 9 but I have not checked if it's currently the case.

    EDIT - they are.

This discussion has been closed.