Hi, is there a way to open a Folder selector when ...
# compose-desktop
a
Hi, is there a way to open a Folder selector when clicking a button, then register the path to a
MutableState<String>
? I have pretty much all steps done but I don't know how to create a Folder selector ? (I know there is java.awt.FileDialog but I can't find anywhere how to select folders with it)
e
Copy code
val chooser = JFileChooser()
    chooser.fileSelectionMode = JFileChooser.DIRECTORIES_ONLY
    val returnVal = chooser.showOpenDialog(frame)
    if(returnVal == JFileChooser.APPROVE_OPTION) {
      frame.dispose()
      currentDirectory = chooser.selectedFile
    } else {
      currentDirectory = null
    }
a
What is
frame
?
e
It's a JFrame
Copy code
val frame = JFrame().apply {
      defaultCloseOperation = JFrame.EXIT_ON_CLOSE
      val screenBounds = graphicsConfiguration.bounds
      val x = (screenBounds.width - width) / 2 + screenBounds.x
      val y = (screenBounds.height - height) / 2 + screenBounds.y
      setLocation(x, y)
      isVisible = true
    }
a
But is there a way to use the default file selector from the OS instead of the horrible one from Swing ?
e
If you use
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
it's almost the same
a
Oh I didn't know there was this thing, thanks !