Hi, getting this exception when trying to open a J...
# compose-desktop
r
Hi, getting this exception when trying to open a JFileChooser:
Copy code
val scope = rememberCoroutineScope()
val fileChooser = remember {
    JFileChooser()
}
Button(onClick = {
   scope.launch {
        val result = fileChooser.showOpenDialog(null)
    }
}, content = { Text("Click to crash") })
Fatal exception in coroutines machinery for DispatchedContinuation[FlushCoroutineDispatcher@478b4af6, Continuation at androidx.compose.foundation.gestures.TapGestureDetectorKt$detectTapAndPress$2$1$4.invokeSuspend(TapGestureDetector.kt)@40916d9b]. Please read KDoc to 'handleFatalException' method and report this incident to maintainers"
This started with Compose version 1.7.0 Beta02. Coroutines 1.9.0-RC2 It only happens if I show the fileChoose dialog inside the scope. (The reason for the scope is other code that I have which I did not include). Crashlog in thread.
solved 1
Untitled.json
a
Can you put the showOpenDialog in a try..catch and see if it throws anything?
r
Copy code
try {
                scope.launch {
                    try {
                        val result = fileChooser.showOpenDialog(null)
                    } catch (e: Throwable) {
                        e.printStackTrace()
                    }
                }
            } catch (e: Throwable) {
                e.printStackTrace()
            }
Nope, neither catch caught anything
Interestingly,
Copy code
scope.launch(<http://Dispatchers.IO|Dispatchers.IO>)
seems to solve the issue. Though I would consider it just a workaround...
a
I don’t know exactly why that exception is being thrown, but it seems like a bad way to open a file chooser.
showOpenDialog
blocks the calling thread, which in your case is the main event dispatching thread. It means all the UI is going to be “stuck” while your dialog is open. The exception is probably related to that, seeing as it goes away if you use a different thread. I’d suggest to do it like this
Copy code
fun main() = singleWindowApplication {
    Column {
        var showFileChooser by remember { mutableStateOf(false) }
        Button(
            onClick = { showFileChooser = true },
            content = { Text("Click to crash") }
        )

        CircularProgressIndicator()

        if (showFileChooser) {
            DisposableEffect(Unit) {
                val chooser = JFileChooser()
                val thread = thread {
                    chooser.showOpenDialog(null)
                }
                onDispose {
                    chooser.cancelSelection()
                    thread.join()
                }
            }
        }
    }
}
You can see that the circular progress indicator will continue running while the dialog is open. In your case it will not.
r
yeah, you are right, I refactored the code to something similar to yours to launch in a separate thread. Works fine now, thanks!
👍🏾 1
👍 1