问题
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?
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?
回答1:
Use the old java.awt.FileDialog instead:
new java.awt.FileDialog((java.awt.Frame) null).setVisible(true);
回答2:
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!
回答3:
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());

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.
回答4:
The SWT components have always looked the same styles that are in the running OS. You can see some examples:
- File Dialog Example : Dialog « SWT JFace Eclipse « Java
- eclipse.platform.swt.git - Eclipse SWT
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.
来源:https://stackoverflow.com/questions/10745198/how-to-use-the-default-file-chooser-for-the-operating-system-java