SwingPanel
throws NPE on focus request if nothing focusable
#2512
Labels
SwingPanel
throws NPE on focus request if nothing focusable
#2512
Kotlin: 1.7.20
Compose: 1.2.1
If there's no component focusable inside a
SwingPanel
and you try to request focus to it via, e.g., tabbing, it'll crash the application.Try the following sample code,
Outcome:
Relevant stack trace:
Now, in the above sample code, set
letsSetItToFocusable = true
instead, and it won't crash anymore. That's because theSwingPanel
now has a focusable component.Digging into the source code of
SwingPanel
, I found the following lines to be problematic:https://github.com/JetBrains/androidx/blob/release/1.2.1/compose/ui/ui/src/desktopMain/kotlin/androidx/compose/ui/awt/SwingPanel.desktop.kt#L190
https://github.com/JetBrains/androidx/blob/release/1.2.1/compose/ui/ui/src/desktopMain/kotlin/androidx/compose/ui/awt/SwingPanel.desktop.kt#L203
The problem is, both
getFirstComponent(…)
andgetLastComponent(…)
of theFocusTraversalPolicy
interface are allowed to returnnull
, and yet the kotlin code is treating it that it won't. This was confirmed via a debugger, and also if you use the IDE to look for implementations of theFocusTraversalPolicy
interface, you will find that it may indeed return null when no swing component is focusable.Suggestion: Honor the nullability of those methods, and perhaps simply skip over the
SwingPanel
if it has no focusable components when looking for components to focus to.The text was updated successfully, but these errors were encountered: