Home > Software engineering >  How to create a simple folder chooser with swing like this?
How to create a simple folder chooser with swing like this?

Time:10-02

I'm learning java swing recently. Please don't tell me it's out of date. I know. I want to create a folder selector like this, there is nothing extra.

browse

Instead of the following, there are many other elements on it browse file

CodePudding user response:

java swing has a jfilechooser which you can use. refer Java Swing Here

CodePudding user response:

You can use JavaFX:

DirectoryChooser directoryChooser = new DirectoryChooser();
directoryChooser.setInitialDirectory(new File("src"));

Button button = new Button("Select Directory");
button.setOnAction(e -> {
    File selectedDirectory = directoryChooser.showDialog(primaryStage);
    System.out.println(selectedDirectory.getAbsolutePath());
 });

Javadoc: https://docs.oracle.com/javase/10/docs/api/javafx/stage/DirectoryChooser.html

It does indeed call the Windows browse for folder dialog:

JavaFX src: https://github.com/openjdk/jfx/blob/86b854dc367fb32743810716da5583f7d59208f8/modules/javafx.graphics/src/main/native-glass/win/CommonDialogs_Standard.cpp#L368

Win32 docs: https://docs.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shbrowseforfolderw

  • Related