Lana Del Slay
Lana Del Slay

Reputation: 41

FileDialog hanging on Mac OS in JFX Application

So far, I've tried to run this on a fresh Mojave, and my main Catalina installation with the same hanging results. The code snippet to open FileDialog runs fine independently in a separate project, but if I create another class in the same package and call it like that, it's the same deal - still hanging. It's being called within the controller if that makes a difference (I've tried JFileChooser which also hangs, and seems to have a history of hanging on OS X. To reiterate I also tried extracting the code to open the dialog to a different class aswell.) It hangs when it reaches fd.setVisible(true);. I've tried disabling iCloud as I've heard that can cause issues, I've also disabled SIPS as a test. I'm not even sure how I can debug this further now. Any help would be super appreciated!

            JFrame frame = new JFrame();
            System.setProperty("apple.awt.fileDialogForDirectories", "true");
            FileDialog fd = new FileDialog(frame, "Choose a file", FileDialog.LOAD);
            fd.setVisible(true);
            String filename = fd.getFile();
            System.out.println(filename);

Upvotes: 0

Views: 332

Answers (3)

user29506073
user29506073

Reputation: 1

I was having the same problem on Mac OSX, putting in an Runnable solved for me.

private void fileChooserMethod() {
    EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        javax.swing.JFileChooser fileChooser = new JFileChooser();
        int response =  fileChooser.showOpenDialog(null);
        //rest of code here
    }
});
}

Upvotes: 0

Julie Megan
Julie Megan

Reputation: 1

Execute dispose() on the JFrame and FileDialog when finished. e.g.

JFrame frame = new JFrame();
System.setProperty("com.apple.macos.use-file-dialog-packages", "true");
FileDialog fd = new FileDialog(frame, "choose_file", FileDialog.LOAD);
fd.setVisible(true);
String filename = fd.getFile();           
frame.dispose();
fd.dispose();

Alternatively, you can try System.exit(). e.g.

JFrame frame = new JFrame();
System.setProperty("com.apple.macos.use-file-dialog-packages", "true");
FileDialog fd = new FileDialog(frame, "choose_file", FileDialog.LOAD);
fd.setVisible(true);
String filename = fd.getFile();           
System.exit();

There is some debate as to which approach is better here.

Upvotes: 0

Lana Del Slay
Lana Del Slay

Reputation: 41

OMG. It only took me all day, but I only needed to make the FileDialog run on it's own thread. Hopefully this'll save some other noob's time lol. My new code looks like this:

public class threadTest extends Thread {
    public void run(){  
        JFrame frame = new JFrame();
        System.setProperty("com.apple.macos.use-file-dialog-packages", "true");
        FileDialog fd = new FileDialog(frame, "choose_file", FileDialog.LOAD);
        fd.setVisible(true);
        String filename = fd.getFile();
      }    
}

and I called it like this

 threadTest test=new threadTest();   
 test.start(); 

Upvotes: 0

Related Questions