JavaFX ContextMenu does not hide

I have a JavaFX ContextMenu assigned with the right mouse button using a scrollpane. It opens, but it doesn't close when you go out of the scroll area. I could add another mouse event to the scrollpane to hide it, but that only solves one problem. The main problem is that when I click on any scroll component, the context menu stays open.

Example. Open the pop-up window with the right mouse button, then click the button. The popup menu is still open.

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollPane;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        final ContextMenu contextMenu = new ContextMenu();

        MenuItem item1 = new MenuItem("About");
        MenuItem item2 = new MenuItem("Preferences");

        contextMenu.getItems().addAll(item1, item2);


        Rectangle rect = new Rectangle( 100,100,150,150);
        Button button = new Button( "Button Text");

        // create nodes
        Group root = new Group();
        root.getChildren().add( rect);
        root.getChildren().add( button);

        // create scrollpane
        ScrollPane sp = new ScrollPane( root);
        sp.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {

                if (event.isSecondaryButtonDown()) {
                    contextMenu.show( sp, event.getScreenX(), event.getScreenY());
                } 
            }
        });



        // create scene
        Scene scene = new Scene(sp, 400, 400, Color.WHITE);

        // add scene to primary stage
        primaryStage.setScene( scene);
        primaryStage.show();
    }
}

      

enter image description here

The documentation says there is a setAutoHide method, but that doesn't work in my case:

Specifies whether pop-ups should be automatically hidden. If the popup loses focus and autoHide is true, then the popup will be hidden automatically. The only exception is the owner of the Node using show (javafx.scene.Node, double, double). Node focus owner will not hide PopupWindow.

@defaultValue false

Many thanks!

+3


source to share


1 answer


Interaction with the children of the parent will focus on that parent. Therefore, the context menu will not hide when the button is clicked in your code.

Try these two approaches:
1) Manually control the visibility of the context menu, i.e. Hide it by pressing a button:

button.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent arg0) {
        contextMenu.hide();
    }
});

      



2) Use setContextMenu () instead of displaying a context menu on a mouse click event:

sp.setContextMenu(contextMenu);

      

+7


source







All Articles