Reputation: 23
I am struggling with JavaFX StackPane event handling.
When I move mouse over my stackpane then MapController.onMouseMoved() is called and this is clear for me.
The webpage has script that should handle onmousemove event. The script works when my WebView is on the top in my StackPane or when the Pane placed over the WebView is mouseTransparent. This is the case when event target is my WebView.
My problem:
I need to handle the case when the event target is my Pane like in the fxml fragment. I need to resend the mouse move event also to sibling WebView in order to execute the script within the webpage. In other words I want to trigger another mouse event from within the WebView. Please respond with verified examples based on fxml fragment below.
<StackPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="foo.bar.MapController">
<WebView fx:id="m_projectsWebView" prefHeight="600.0" prefWidth="800.0"/>
<Pane fx:id="m_devicesLayer" onMouseMoved="#onMouseMoved"/>
</StackPane>
Upvotes: 0
Views: 938
Reputation: 23
I found a solution a while ago and below is the fragment of code for those who face similar problems.
/** redirect events to a sibling node (m_projectsWebView) */
m_devicesLayer.addEventHandler(Event.ANY, event -> {
if (!(event instanceof MouseEvent) {
m_projectsWebView.fireEvent(event.copyFor(m_projectsWebView, m_projectsWebView));
}
event.consume();
});
I just intercept all events on my Pane level, then I create copy for its sibling (WebView), and then I consume the original event to prevent it from propagating.
All required events for the components belonging to the Pane are handled and my WebView receives required events as well.
Upvotes: 2