Windows native File chooser in java

2020-02-05 06:16发布

问题:

Apparently, there are (at least?) two different native File choosers on Windows (10). There is this one, which is used by JFileChooser and other programs:

And there is that one, for example used by Chrome:

I like it much more than the first one because:

  • You can directly enter your file path at the top
  • Your can search the folder
  • The direct access on the left contains the whole file tree

How do I get it in Java?

回答1:

Use the JavaFX library

FileChooser fileChooser = new FileChooser();
fileChoose.showOpenDialog(null);

To run it in a swing context, have a look at those two answers.

PlatformImpl.startup(() -> {
    FileChooserd = new FileChooser();
    d.showOpenDialog(null);
});

 new JFXPanel();
 Platform.runLater(() -> {
     FileChooser d = new FileChooser();
     d.showOpenDialog(null);
 });

Note that other things like modality won't work when mixing JavaFX and Swing. Also, you will have to build some code that waits until the Runnable has finished to be able to fetch the results.

Use a special library

native file dialogs provides native file dialogs, and LWJGL 3 provides Java bindings for this library.