Javafx : Disabling Menu (MenuButton) auto close

936 Views Asked by At

I have a MenuButton that contains only CheckMenuItems. My user will usually check several items and if he has to re-open the menu for each one, he will soon throw his mouse through the screen.

I choose to use a menubutton rather than a combobox because it seems that it's not possible to put checkboxes into a combobox (https://community.oracle.com/thread/2598157).

Anyone has an idea ? Thank you very much, Léo

2

There are 2 best solutions below

0
On

Consider in addition to the menu items to provide a toolbar with toggle buttons.

Note: Drombler FX provides an action framework to keep the state and logic between menu items and toolbar buttons in sync. It supports CheckMenuItems and toggle buttons as well.

Disclaimer: I'm the author of Drombler FX.

Getting Started: http://wiki.drombler.org/GettingStarted

Blog: http://puces-blog.blogspot.ch/search/label/Drombler

0
On

This worked for me:

@FXML
public void autoShow() {

    checkmenuitem.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent e) {
           e.consume();
    }
});


}

checkmenuitem is the id. When it is clicked, the handle method is executed. For a detailed explanation on the event.consume() method, see this:

What is the meaning of Event consumes in JavaFX

Place the above method in your controller class and then call it from the initialize method in your controller class:

@Override 
public void initialize(URL location, ResourceBundle resources) {

    autoShow();

}