diff --git a/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/ContextMenuContent.java b/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/ContextMenuContent.java --- a/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/ContextMenuContent.java +++ b/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/ContextMenuContent.java @@ -1035,6 +1035,8 @@ private EventHandler mouseEnteredEventHandler; private EventHandler mouseReleasedEventHandler; + private EventHandler actionEventHandler; + protected Label getLabel(){ return (Label) label; } @@ -1080,7 +1082,7 @@ listener.registerChangeListener(item.graphicProperty(), "GRAPHIC"); - addEventHandler(ActionEvent.ACTION, e -> { + actionEventHandler = e -> { if (item instanceof Menu) { final Menu menu = (Menu) item; if (openSubmenu == menu && submenu.isShowing()) return; @@ -1093,12 +1095,20 @@ } else { doSelect(); } - }); - + }; + addEventHandler(ActionEvent.ACTION, actionEventHandler); } public void dispose() { + if (item instanceof CustomMenuItem) { + Node node = ((CustomMenuItem)item).getContent(); + if (node != null) { + node.removeEventHandler(MouseEvent.MOUSE_CLICKED, customMenuItemMouseClickedHandler); + } + } + listener.dispose(); + removeEventHandler(ActionEvent.ACTION, actionEventHandler); if (label != null) { ((Label)label).textProperty().unbind(); @@ -1304,19 +1314,23 @@ item.fire(); hideAllMenus(item); } + + private EventHandler customMenuItemMouseClickedHandler; private void createNodeMenuItemChildren(final CustomMenuItem item) { Node node = item.getContent(); getChildren().add(node); + // handle hideOnClick - node.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> { + customMenuItemMouseClickedHandler = event -> { if (item == null || item.isDisable()) return; item.fire(); if (item.isHideOnClick()) { hideAllMenus(item); } - }); + }; + node.addEventHandler(MouseEvent.MOUSE_CLICKED, customMenuItemMouseClickedHandler); } @Override protected void layoutChildren() {