5

I've been making a program that uses a JFileChooser. I've set the application up with

UIManager.getSystemLookAndFeelClassName()

Which works just fine for pretty much everything under Ubuntu. The only problem I've run into so far is that the JFileChooser comes out looking pretty awful: JFileChooser with SystemLookAndFeel

Is there a way to make this look like the default file chooser in Ubuntu? ie. Desired file chooser dialog

I've tried using

UIManager.getCrossPlatformLookAndFeelClassName()

Which makes the JFileChooser dialog look better, but still not native looking, and it ruins the rest off the application's feel too.

Thanks.

skaffman
  • 398,947
  • 96
  • 818
  • 769
joshschreuder
  • 1,413
  • 2
  • 18
  • 32
  • Refer to below link for the solution: http://stackoverflow.com/questions/10597831/improving-jfilechooser-under-ubuntu-12-04-gtk – Gautam Chauhan Apr 26 '13 at 19:41

4 Answers4

2

If I recall correctly, the stock JDK used gtk1, but ubuntu uses gtk2 currently. I forget where but i've come across gtk2 for java somewhere. Google? Probably not what you were hoping for, sorry.

jwp
  • 138
  • 6
  • Searching this did help, I ran into this: https://code.google.com/p/gtkjfilechooser/ . Not sure how I can make this perform cross-platform, but I'll look more into it. Thanks for your help. – joshschreuder Feb 14 '11 at 04:18
  • You could probably do what jzd was suggesting you do with nimbus and add this as a seperate lib in your project and only use the look on systems that want a gtk native look. – jwp Feb 15 '11 at 11:46
1

You might see if FileDialog is any more appealing; here's an example.

FileDialog

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

The Nimbus look and feel has a decent file chooser. Although this will affect your entire application, you might like the look.

Also you can build your own file chooser if needed.

jzd
  • 23,473
  • 9
  • 54
  • 76
  • Nimbus was only added in Java 6 update 10, I think. I'm trying to stay away from forcing specific Java versions (might be able to do a fall-back somehow?), but I'll definitely look into this, thanks. – joshschreuder Feb 14 '11 at 04:20
  • 1
    Before it was included in the JRE it was a separate library that you would include in your project. That would work for older versions of Java. – jzd Feb 14 '11 at 12:06
0

You can also use SWT instead of swing.

Does Swing support Windows 7-style file choosers?

The following code is from above link

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;

public class SWTFileOpenSnippet {
    public static void main (String [] args) {
        Display display = new Display ();
        Shell shell = new Shell (display);
        // Don't show the shell.
        //shell.open ();  
        FileDialog dialog = new FileDialog (shell, SWT.OPEN | SWT.MULTI);
        String [] filterNames = new String [] {"All Files (*)"};
        String [] filterExtensions = new String [] {"*"};
        String filterPath = "c:\\";
        dialog.setFilterNames (filterNames);
        dialog.setFilterExtensions (filterExtensions);
        dialog.setFilterPath (filterPath);
        dialog.open();
        System.out.println ("Selected files: ");
        String[] selectedFileNames = dialog.getFileNames();
        for(String fileName : selectedFileNames) {
            System.out.println("  " + fileName);
        }
        shell.close();
        while (!shell.isDisposed ()) {
            if (!display.readAndDispatch ()) display.sleep ();
        }
        display.dispose ();
    }
}
Community
  • 1
  • 1