How to use the default file chooser for the operating system?
Asked Answered
M

4

20

I was just wondering: how does Gmail use the Windows/Mac file chooser to upload files? Is there any way to do this in Java?

enter image description here

Personally, I don't like the way that the JFileChooser looks like, and I thought it would be better for my users to be able to use something that they're more used to. Tips anyone?

Mccammon answered 24/5, 2012 at 20:59 Comment(1)
Take a look at AWT's FileDialogReptile
S
22

Use the old java.awt.FileDialog instead:

new java.awt.FileDialog((java.awt.Frame) null).setVisible(true);
Sheikdom answered 24/5, 2012 at 21:7 Comment(8)
Thank you! I will use this! I never really thought about components from java.awt.*.Mccammon
Well we used to think about them a lot back in the day, before swing existed. :)Sheikdom
Although this answer is quite old: I wouldn't recommend using the Awt file dialog. It's broken on Linux and Mac, and Oracle is not going to fix it any more. So, unless you are only targeting Window users, this is not a good idea. Besides, JFileChooser with the native look and feel looks almost identically on Windows.Towhee
While that's true @Aru the JFileChooser even with native look is pretty far from the native file chooser in looks and functionality and slower (at least used to be). I fair compromise in some circumstances can be to use check for windows and use that.Sheikdom
This implementation doesn't allow you to provide file filter on Windows. For example, you cannot force the dialog to show only *.jpg files. As far as I can see from Java 9 docs, it's still not implemented. Do you know any other APIs for that?Clown
No unfortunately no file filters etc, very plain api. The JavaFX FileDialog is much better than the swing one IMO though and it's simple to mix them (different ui thread though) but it is supported to mix JavaFX and swing fairly well. SWT would be another option.Sheikdom
This looks way better: https://mcmap.net/q/514599/-how-to-use-filedialogBurschenschaft
This answer is outdated. FileDialog now provides an outdated file chooser on Windows, not the one used by the current OS.Symphonia
D
15

You can try using JFileChooser but setting the look and feel to be the platform look and feel:

    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }catch(Exception ex) {
        ex.printStackTrace();
    }

And that would make all the swing components look nicer!

Dumfound answered 24/5, 2012 at 21:11 Comment(1)
It has plenty of other problems outside of the look and feel though, it doesn't give access to your "Quick access" directories in windows and just isn't convenient to navigate withMccue
T
6

GMail is a web application that eventually relies on the browser to show this component. Now a good solution is to use the Native Look&Feel of the system which provides a JFileChooser quite similar to what you show:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

enter image description here

EDIT: Pulsar's solution is even better since it provides the exact dialog you are looking for. I am not sure that it provides all the features of the JFileChooser.

Tertia answered 24/5, 2012 at 21:8 Comment(0)
L
3

The SWT components have always looked the same styles that are in the running OS. You can see some examples:

It was assumed that from version 7 of Java, Swing styles would be more like that of operating systems, but may see it in Java 8.

Lenora answered 24/5, 2012 at 21:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.