To save one handler instance / control instance one could make the event-handler static and calculate the control from the event information.
diff --git a/modules/controls/src/main/java/javafx/scene/control/Control.java b/modules/controls/src/main/java/javafx/scene/control/Control.java
--- a/modules/controls/src/main/java/javafx/scene/control/Control.java
+++ b/modules/controls/src/main/java/javafx/scene/control/Control.java
@@ -167,13 +167,18 @@
* classes which we'd otherwise have to create. When lambda expressions
* are supported, we could do it that way instead (or use MethodHandles).
*/
- private final EventHandler<ContextMenuEvent> contextMenuHandler = new EventHandler<ContextMenuEvent>() {
+ private final static EventHandler<ContextMenuEvent> contextMenuHandler = new EventHandler<ContextMenuEvent>() {
@Override public void handle(ContextMenuEvent event) {
// If a context menu was shown, consume the event to prevent multiple context menus
- if (getContextMenu() != null) {
- getContextMenu().show(Control.this, event.getScreenX(), event.getScreenY());
- event.consume();
- }
+ Object source = event.getSource();
+ if( source instanceof Control ) {
+ Control c = (Control) source;
+ if (c.getContextMenu() != null) {
+ c.getContextMenu().show(c, event.getScreenX(), event.getScreenY());
+ event.consume();
+ }
+ }
+
}
};
diff --git a/modules/controls/src/main/java/javafx/scene/control/Control.java b/modules/controls/src/main/java/javafx/scene/control/Control.java
--- a/modules/controls/src/main/java/javafx/scene/control/Control.java
+++ b/modules/controls/src/main/java/javafx/scene/control/Control.java
@@ -167,13 +167,18 @@
* classes which we'd otherwise have to create. When lambda expressions
* are supported, we could do it that way instead (or use MethodHandles).
*/
- private final EventHandler<ContextMenuEvent> contextMenuHandler = new EventHandler<ContextMenuEvent>() {
+ private final static EventHandler<ContextMenuEvent> contextMenuHandler = new EventHandler<ContextMenuEvent>() {
@Override public void handle(ContextMenuEvent event) {
// If a context menu was shown, consume the event to prevent multiple context menus
- if (getContextMenu() != null) {
- getContextMenu().show(Control.this, event.getScreenX(), event.getScreenY());
- event.consume();
- }
+ Object source = event.getSource();
+ if( source instanceof Control ) {
+ Control c = (Control) source;
+ if (c.getContextMenu() != null) {
+ c.getContextMenu().show(c, event.getScreenX(), event.getScreenY());
+ event.consume();
+ }
+ }
+
}
};