improving JFileChooser under Ubuntu 12.04 (GTK)
Asked Answered
P

3

5

I have a problem with the JFileChooser under Ubuntu 12.04. I use this code to set the look and feel: javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());

And it looks like this. It's very uncomfortable to use and it looks very ugly:

enter image description here
(source: picfront.org)

I would like it to look like this:

enter image description here
(source: picfront.org)

Using the hint from this post, I tried to use FileDialog instead of the FileChooser. But FileDialog throws an exception when i start it in LOAD mode and click on the "open" button. The way i create the Dialog:

FileDialog fd = new FileDialog(frame, "Test", FileDialog.LOAD);
fd.setVisible(true);

The Exception:

Exception in thread "Thread-0" java.lang.NullPointerException
at sun.awt.X11.GtkFileDialogPeer.setFileInternal(GtkFileDialogPeer.java:79)
at sun.awt.X11.GtkFileDialogPeer.run(Native Method)
at sun.awt.X11.GtkFileDialogPeer.showNativeDialog(GtkFileDialogPeer.java:172)
at sun.awt.X11.GtkFileDialogPeer.access$000(GtkFileDialogPeer.java:39)
at sun.awt.X11.GtkFileDialogPeer$1.run(GtkFileDialogPeer.java:114)

I am using Oracle JDK7 under Ubuntu Linux 12.04 with Gnome 3 (if this helps).

Does anybody got an idea how either i could improve the look of the JFileChooser or get the FileDialog working?

Puisne answered 15/5, 2012 at 9:27 Comment(2)
no idea why you got an exeption, I haven't any issue on Win OS, for potential answerers you have to edit your question with a SSCCE, there are a few answerers using *nix clones,Melany
There is an image button to embed a picture.Carnarvon
R
3

I had the same problem in a Java GUI project I was working on. I set it to use "zenity" terminal program to call the native file-chooser on Linux/unix systems. Note that this solution does not require importing any extra Java libraries (You must have Zenity installed on Linux though), and also works fine on Windows:

private File fileSelection(boolean savemode) {
        String os = System.getProperty("os.name");
        File input = null;
        String zenity = "zenity --file-selection --title=Open";
        String filestring;
        if ((os.indexOf("nix")!=-1 || os.indexOf("nux")!=-1)) {
            //Use native Linux file selection.
            try {
                if (savemode) {
                    zenity ="zenity --file-selection --title=Save --save";
                }
                Process p = Runtime.getRuntime().exec(zenity);  
                BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));  
                StringBuffer sb = new StringBuffer();  
                String line;
                /*while ((line = br.readLine()) != null) {  
                  sb.append(line).append("\n");  
                } */
                sb.append(br.readLine());
                filestring = sb.toString();  
                if (filestring.equals("null")) {
                    return null;
                }
                System.out.println(filestring);
                input = new File(filestring);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        } else {
            final JFileChooser fc = new JFileChooser();
            int returnVal;
            if (savemode) {
                returnVal = fc.showSaveDialog(fc);
            } else {
                returnVal = fc.showOpenDialog(fc);  
            }
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                input = fc.getSelectedFile();
            }
        }
        return input;
    }
Rhynchocephalian answered 8/8, 2012 at 19:31 Comment(0)
B
1

Just for completeness' sake, here's the java-forum.org thread where you posted about the same question in German.

User eRaaaa posted a fix to this bug, which subsequently was turned into a bug report at bugs.sun.com that was reviewed positively.

Broadbill answered 29/8, 2012 at 17:34 Comment(0)
W
0

A NullPointerException usually means you are pointing to something that is not there. I suppose the reference you try to point to is lost, during runtime.

Webbed answered 15/5, 2012 at 11:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.