If I'm wrapping a Swing component in a SwingPanel,...
# compose-desktop
b
If I'm wrapping a Swing component in a SwingPanel, how can I send native Swing events to modifiers that were passed in? For example, say, the onPreviewKeyEvent Modifier?
i
You can import awtEventOrNull extension. It is available for KeyEvent and for PointerEvent.
b
No, the problem is that the modifiers aren't called when attached to the SwingPanel, so I'm assuming I'd have to do that myself in the Swing code by adding listeners and directly calling the modifiers to connect them together. Is it not possible to see what modifiers were received and work with them manually?
i
Ah, I understand. You are trying to implement something like this:
Copy code
SwingPanel(Modifier.onPreviewKeyEvent = {}, factory = ::SomeSwingComponent)
And clicks on SomeSwingComponent and key presses when SomeSwingComponent is in focus aren’t caught by the upper modifier. Yes, this isn’t implemented unfortunately. The swing event handling tree is detached from the Compose event handling tree at the moment, and the only way to catch the events on the Swing component is to set listeners manually. But I will investigate in the future, if it is possible to make a seamless event tree for Swing/Compose components.
Is it not possible to see what modifiers were received and work with them manually
There can be not only modifiers on SwingPanel, but also on some upper component:
Copy code
Box(Modifier.onPreviewKeyEvent) {
  SwingPanel
}
Such configuration isn’t supported too, and there is no way to pass the event to this modifier, because the event system in Swing doesn’t know about this Compose component, and connecting the Swing event system to this component is probably only possible inside the internals of Compose.
b
That's unfortunate. I ended up just adding my own
on<SomeEvent>
- style inputs that I could end up using in the update method for the SwingPanel. It works, but it sort of breaks the interface from an external user's perspective. It'd be nice if we could inspect the Modifier object for the types that were passed in so that, when creating custom components, we could decide that certain parts of the modifier should be passed to some subcomponent while another is passed to something else.