A MenuItem
is not actually a Node
, so it's not part of the scene graph in the way that Node
s are. So I'm not really sure if this is a bug or not; I think it probably only implements EventTarget
so it can specifically generate ActionEvent
s. You'll have noticed there is no setOnMouseClicked(...)
method available.
Here's a workaround. I'm not sure why it only works with MOUSE_PRESSED
and not with MOUSE_CLICKED
, but it's likely something to do with the default mouse event handling that generates the action events:
private void maakContextMenu() {
menu = new ContextMenu();
MenuItem item = new MenuItem("", new Label("Kleur Assen"));
Label menuItem2Label = new Label("tweede optie");
MenuItem item2 = new MenuItem("", menuItem2Label);
final LissajousCanvas canvas = this;
item.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
new KiesKleur(canvas).show();
}
});
menuItem2Label.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent t) {
System.out.println("in the loop");
if(t.getButton()==MouseButton.SECONDARY){
System.out.println("in too deep");
}
new KiesKleur(canvas).show();
}
});
menu.getItems().addAll(item, item2);
}