0

I create this simple example of a context menu shown when I click with a mouse:

Image image = new Image("http://docs.oracle.com/javafx/"
    + "javafx/images/javafx-documentation.png");
ImageView pic = new ImageView();
pic.setImage(image);

final ContextMenu cm = new ContextMenu();
MenuItem cmItem1 = new MenuItem("Copy Image");
cmItem1.setOnAction(new EventHandler<ActionEvent>()
{
    public void handle(ActionEvent e)
    {
        Clipboard clipboard = Clipboard.getSystemClipboard();
        ClipboardContent content = new ClipboardContent();
        content.putImage(pic.getImage());
        clipboard.setContent(content);
    }
});

cm.getItems().add(cmItem1);
pic.addEventHandler(MouseEvent.MOUSE_CLICKED,
    new EventHandler<MouseEvent>()
    {
        @Override
        public void handle(MouseEvent e)
        {
            if (e.getButton() == MouseButton.SECONDARY)
                cm.show(pic, e.getScreenX(), e.getScreenY());
        }
    });

I noticed that when I click near the picture the context menu is still visible. How I make it hidden when I click outside if the context menu body?

3
  • You want to hide the context menu, when you click which part? Commented Feb 27, 2014 at 11:40
  • I want to hide it when I click everywhere outside the context menu. Commented Feb 27, 2014 at 11:57
  • you will have to implement the keyListener on the screen and check whether the click has been made on the ContextMenu or not ! I will try to frame an example, if I get sometime ! Commented Feb 27, 2014 at 13:05

1 Answer 1

2

set contextMenu.setAutoHide as true.

contextMenu.setAutoHide(true);

Not the answer you're looking for? Browse other questions tagged or ask your own question.