1

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

sukalogika
  • 603
  • 4
  • 11
  • 18
Ulrica Lin
  • 11
  • 2
  • 1
    You could adapt it from the `JTree` on the left in this [File Manager](https://codereview.stackexchange.com/a/4451/7784). – Andrew Thompson Sep 30 '21 at 08:48
  • 1
    I think this -> https://stackoverflow.com/a/4350171/11121568 is the answer to your question – Soroush Shemshadi Sep 30 '21 at 09:34
  • @Soroush Shemshadi It seems not. I want only the folder image, as shown in Figure 1 – Ulrica Lin Sep 30 '21 at 10:07
  • @Andrew Thompson Doesn't Java Swing have ready-made settings? – Ulrica Lin Sep 30 '21 at 10:09
  • 1
    *"Doesn't Java Swing have ready-made settings?"* Plenty of them, but I'm figuring that by 'settings' you mean 'components. The answer to that is 'many, just not the one you seem to want'. Toughen up and use a `JTree`. – Andrew Thompson Sep 30 '21 at 11:04
  • https://stackoverflow.com/questions/23804675/list-files-and-directories-with-jtree-and-file-in-java – Joop Eggen Sep 30 '21 at 14:35
  • Does this answer your question? [Browse for folder dialog](https://stackoverflow.com/questions/4779360/browse-for-folder-dialog) – Charlie Sep 30 '21 at 14:45
  • @Charlie Hello,I know this usage, but it is not the answer I want. What I want is a simple folder selector like Figure 1. It has no extra buttons and input boxes, just select the folder – Ulrica Lin Sep 30 '21 at 21:46
  • @JoopEggen Is it the only way to achieve it by using jtree? – Ulrica Lin Sep 30 '21 at 21:47
  • @UlricaLin Did you try the mode? fileChooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY); – Charlie Oct 01 '21 at 00:18
  • There were some "Explorer" mini-libraries around, but the JFileChooses will not have an extra JTree in the background for just this. Alternative you could add your own JPanel to the original JFileChooser. Either with a JTree to the left, or maybe a clickable bread crumb bar at the top: `C: > Users > Butler > Documents`. – Joop Eggen Oct 01 '21 at 07:25

2 Answers2

0

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

https://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html

  • 1
    I know. But I can only achieve the effect of Figure 2. What I want is the effect of Figure 1. Do you know how to do it? – Ulrica Lin Sep 30 '21 at 08:40
  • please add some code that will make your answer more elaborate and clear for others to come without having to go to external pages – st.huber Sep 30 '21 at 15:34
  • @st.huber Ulrica has shown us a picture what she would like to achieve. Could code add any clarity to this? – Jörg Oct 04 '21 at 19:57
  • @Ulrica Lin Andrew Thompson has given you the link to his FileManager class. Extract the JTree part and most of the work is done. If this is too much for you, have a look at the FileDialog class. It is not giving exactly what you want, but uses the OS's file dialog, and only few lines of code are needed to have it shown. – Jörg Oct 04 '21 at 20:13
  • @Jörg I didn't ask Ulrica for more code but Dawud. Some code that shows how the jfilechooser works would make the answer more useful. Furthermore the `jfilechooser` was mentioned but the link is to the general swing documentation and not to the `jfilechooser`. – st.huber Oct 05 '21 at 06:45
0

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://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shbrowseforfolderw

Charlie
  • 8,530
  • 2
  • 55
  • 53
  • I don't think it is a valid answer for a question starting with "_I'm learning java swing_" – c0der Oct 02 '21 at 05:15
  • Well, good news! You can use Swing and JavaFX together! I looked in the src of the standard runtime and didn't see the appropriate native call, so I don't think there's a swing option outside JNI or third party libraries. – Charlie Oct 02 '21 at 12:45