Is there a way to improve JFileChooser look and feel under Ubuntu?

被刻印的时光 ゝ 提交于 2019-12-07 07:30:50

问题


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:

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

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.


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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 ();
    }
}


来源:https://stackoverflow.com/questions/4983575/is-there-a-way-to-improve-jfilechooser-look-and-feel-under-ubuntu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!