Issue
The package SwingUtilities
has a nice function, convertMouseEvent
, to convert a mouse event on a component to a mouse event on another component:
MouseEvent convertedEvent = SwingUtilities.convertMouseEvent(originalComponent, event, otherComponent);
Meanwhile, the MouseEvent
in JavaFX has a method copyFor
to create a copy of the MouseEvent
for another component:
MouseEvent convertedEvent = e.copyFor(e.getSource(), otherComponent);
I would like to do something similar to convert a JavaFx MouseEvent e
to a Swing mouse event.
I couldn't find a baked in function, so I tried to write my own. There are two fields I can't readily convert though:
- Id field that identifies the event. I assume I'll have to manually convert from
e.getEventType().getEventType().getName()
- In JavaFX there is no "when" property indicating when the event occurred. It seems I could use the current system time here
- The modifier of the mouse event. I'm not sure how to build those from the properties of the JavaFX event
Is there a way to get the modifier from the properties of e
?
Solution
The functions in SwingEvents
can be useful. They can be accessed with the import
import com.sun.javafx.embed.swing.SwingEvents;
To convert from JavaFX to Swing:
SwingEvents.fxMouseButtonToMouseButton(fxEvent)
: converts a JavaFX event to a Swing mouse button.
SwingEvents.fxMouseEventTypeToMouseID(fxEvent)
: converts a JavaFX event to a Swing mouse ID.
SwingEvents.fxMouseModsToMouseMods(fxEvent)
: converts a JavaFX event to Swing mods.
To convert from Swing to JavaFX:
SwingEvents.mouseIDToEmbedMouseType(swingEvent.getID())
: converts a Swing mouse event ID to a JavaFX MouseType.
SwingEvents.mouseButtonToEmbedMouseButton(swingEvent.getButton, swingEvent.getModifiersEx())
: converts a Swing mouse button to a JavaFX mouse button. Currently bugged (JDK-8242419).
Answered By - Guillaume F.