Search code examples
javafxfxmlauto-closepopupmenubutton

How to prevent MenuButton from closing when clicking on the CheckMenuItem (JavaFX)


I'm new with Java, I would like to know how to prevent MenuButton popup from closing when I click on the item, I tried the following function, which I found on this site, but does not seem to do anything. I need this in order to make a clone of my C# application whit the purpose of learning Java.

@FXML
private MenuButton menuButton;

@FXML
    void initialize() {
        CheckMenuItem menuButtonItem1 = new CheckMenuItem("Item 1");
        CheckMenuItem menuButtonItem2 = new CheckMenuItem("Item 2");
        CheckMenuItem menuButtonItem3 = new CheckMenuItem("Item 3");
        menuButtonItem1.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent e) {
           e.consume();
    }
});
        menuButtonItem2.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent e) {
           e.consume();
    }
});
        menuButtonItem3.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent e) {
           e.consume();
    }
});
}

Solution

  • The CustomMenuItem class provides a setHideOnClick() method that will handle this. In order to use it, you'll wrap a standard Node in this CustomMenuItem:

    CheckBox checkBox = new CheckBox("Item 1");
    CustomMenuItem customMenuItem = new CustomMenuItem(checkBox);
    customMenuItem.setHideOnClick(false);
    

    Below is a full example for you to try:

    import javafx.application.Application;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.CheckBox;
    import javafx.scene.control.CustomMenuItem;
    import javafx.scene.control.MenuButton;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    public class CheckMenuKeepOpen extends Application {
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) {
    
            // Simple Interface
            VBox root = new VBox(10);
            root.setAlignment(Pos.CENTER);
            root.setPadding(new Insets(10));
    
            // Create the MenuButton
            MenuButton menuButton = new MenuButton("Click Me");
    
            for (int i = 0; i < 5; i++) {
    
                // In order to keep the menu open when selecting a CheckBox, we need to wrap it in a CustomMenuItem
                CustomMenuItem menuItem = new CustomMenuItem(new CheckBox("Item #" + i));
    
                // This method is pretty obvious; it keeps the menu open when selecting this item.
                menuItem.setHideOnClick(false);
                menuButton.getItems().add(menuItem);
            }
    
            root.getChildren().add(menuButton);
    
            // Show the stage
            primaryStage.setScene(new Scene(root));
            primaryStage.setTitle("Sample");
            primaryStage.show();
        }
    }