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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
mattbdean
  • 2,532
  • 5
  • 26
  • 58
  • 2
    Take a look at AWT's [FileDialog](http://docs.oracle.com/javase/7/docs/api/java/awt/FileDialog.html) – Jeffrey May 24 '12 at 21:06

4 Answers4

23

Use the old java.awt.FileDialog instead:

new java.awt.FileDialog((java.awt.Frame) null).setVisible(true);
Mattias Isegran Bergander
  • 11,811
  • 2
  • 41
  • 49
  • 1
    Thank you! I will use this! I never really thought about components from `java.awt.*`. – mattbdean May 24 '12 at 22:16
  • 1
    Well we used to think about them a lot back in the day, before swing existed. :) – Mattias Isegran Bergander May 24 '12 at 22:17
  • 4
    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. – Steffen Apr 09 '15 at 20:03
  • 1
    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. – Mattias Isegran Bergander Apr 13 '15 at 20:40
  • 1
    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? – Ivan Nikitin Oct 05 '16 at 09:07
  • 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. – Mattias Isegran Bergander Oct 05 '16 at 10:11
  • This looks way better: https://stackoverflow.com/questions/7211107/how-to-use-filedialog – trinity420 Jul 05 '18 at 17:30
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!

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.

Community
  • 1
  • 1
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
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.

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148