This site requires JavaScript, please enable it in your browser!
Greenfoot back
MrBradley
MrBradley wrote ...

2017/5/31

Using JFileChooser in Greenfoot

MrBradley MrBradley

2017/5/31

#
I have an interesting situation when using a JFileChooser FileOpen Dialog. It works 100% on windows, but when I test it on a macbook pro, the app hangs. Any help would be appreciated. Thanks.
    private static String previousDirectory = "images";
    private static String previousFilename;
    
    public final void readWorld()
    {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setCurrentDirectory(new File(previousDirectory));
        int result = fileChooser.showOpenDialog(null);

        if (result == JFileChooser.APPROVE_OPTION)
        {
            File file = fileChooser.getSelectedFile();
            previousDirectory=file.getParentFile().getName();
            previousFilename=file.getName();            
            System.out.println("Selected file: " + previousDirectory + previousFilename );
        }
    }
I have a skeleton greenfoot app with pretty much this in it.
MrBradley MrBradley

2017/6/1

#
Apparently this is a known issue. This may be the issue: http://bugs.java.com/view_bug.do?bug_id=7172752 === Fix ===
try { 
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
 } 
catch(Exception e) { System.out.println("Error setting Java LAF: " + e); }
Thanks to SupZ's Blog from 2012. (http://supunmali-myexperience.blogspot.ca/2012/10/issue-jfilechooser-on-mac-os-x.html)
Yehuda Yehuda

2017/6/5

#
Your fix code just changes the appearance of the control(s). I'm not saying I had your problem and I tried your fix, what I'm saying is I use that code when I want to change the Look and Feel from the Java Metal L&F to the one of the system.
        try {
            // Set System L&F
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (UnsupportedLookAndFeelException e) {
            // handle exception
        } catch (ClassNotFoundException e) {
            // handle exception
        } catch (InstantiationException e) {
            // handle exception
        } catch (IllegalAccessException e) {
            // handle exception
        }
I'm not even sure what your problem is (to know how changing the L&F should help).
You need to login to post a reply.