From 1767e6212a6d749f193995ba328cad1f5527eb55 Mon Sep 17 00:00:00 2001 From: Kenneth VanderLinde Date: Thu, 24 Aug 2023 14:45:56 -0700 Subject: [PATCH] Use a new instance each time we add an EventListener to a WebView Using a method reference creates a new `EventListener` instance each time rather than registering the same instance over and over. This works around the JavaFX 19 & 20 bug that generates a NPE while handling events. --- .../ui/htmlframe/HTMLWebViewManager.java | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/main/java/net/rptools/maptool/client/ui/htmlframe/HTMLWebViewManager.java b/src/main/java/net/rptools/maptool/client/ui/htmlframe/HTMLWebViewManager.java index 504eccc587..44a9a510b9 100644 --- a/src/main/java/net/rptools/maptool/client/ui/htmlframe/HTMLWebViewManager.java +++ b/src/main/java/net/rptools/maptool/client/ui/htmlframe/HTMLWebViewManager.java @@ -49,7 +49,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.w3c.dom.*; -import org.w3c.dom.events.EventListener; import org.w3c.dom.events.EventTarget; import org.w3c.dom.html.*; @@ -79,11 +78,6 @@ public class HTMLWebViewManager { /** The bridge from Javascript to Java. */ private final JavaBridge bridge; - // Event listener for the href macro link clicks. - private final EventListener listenerA = this::fixHref; - // Event listener for form submission. - private final EventListener listenerSubmit = this::getDataAndSubmit; - /** Represents a bridge from Javascript to Java. */ public class JavaBridge { @@ -152,11 +146,11 @@ public void handleAddedNode(Object object) { if (addedNode instanceof EventTarget) { EventTarget target = (EventTarget) addedNode; if (addedNode instanceof HTMLAnchorElement || addedNode instanceof HTMLAreaElement) { - target.addEventListener("click", listenerA, true); + target.addEventListener("click", HTMLWebViewManager.this::fixHref, true); } else if (target instanceof HTMLFormElement) { - target.addEventListener("submit", listenerSubmit, true); + target.addEventListener("submit", HTMLWebViewManager.this::getDataAndSubmit, true); } else if (target instanceof HTMLInputElement || target instanceof HTMLButtonElement) { - target.addEventListener("click", listenerSubmit, true); + target.addEventListener("click", HTMLWebViewManager.this::getDataAndSubmit, true); } } @@ -167,34 +161,34 @@ public void handleAddedNode(Object object) { nodeList = addedNode.getElementsByTagName("a"); for (int i = 0; i < nodeList.getLength(); i++) { EventTarget node = (EventTarget) nodeList.item(i); - node.addEventListener("click", listenerA, true); + node.addEventListener("click", HTMLWebViewManager.this::fixHref, true); } // Add event handlers for hyperlinks for maps. nodeList = addedNode.getElementsByTagName("area"); for (int i = 0; i < nodeList.getLength(); i++) { EventTarget node = (EventTarget) nodeList.item(i); - node.addEventListener("click", listenerA, true); + node.addEventListener("click", HTMLWebViewManager.this::fixHref, true); } // Set the "submit" handler to get the data on submission not based on buttons nodeList = addedNode.getElementsByTagName("form"); for (int i = 0; i < nodeList.getLength(); i++) { EventTarget target = (EventTarget) nodeList.item(i); - target.addEventListener("submit", listenerSubmit, true); + target.addEventListener("submit", HTMLWebViewManager.this::getDataAndSubmit, true); } // Set the "submit" handler to get the data on submission based on input nodeList = addedNode.getElementsByTagName("input"); for (int i = 0; i < nodeList.getLength(); i++) { EventTarget target = (EventTarget) nodeList.item(i); - target.addEventListener("click", listenerSubmit, true); + target.addEventListener("click", HTMLWebViewManager.this::getDataAndSubmit, true); } // Set the "submit" handler to get the data on submission based on button nodeList = addedNode.getElementsByTagName("button"); for (int i = 0; i < nodeList.getLength(); i++) { EventTarget target = (EventTarget) nodeList.item(i); - target.addEventListener("click", listenerSubmit, true); + target.addEventListener("click", HTMLWebViewManager.this::getDataAndSubmit, true); } } }