I have had a problem concerning the JFileChooser for a long while now and haven't been able to find help... The problem is that the file window is not showing up. I have tried to find the cause of this problem and I have tested the following:
public class Test {
public static void main (String[] args) {
load();
}
public static void load () {
String folder = System.getProperty("user.dir");
JFileChooser fc = new JFileChooser(folder);
int resultat = fc.showOpenDialog(null);
}
}
When running this code I do get the window to show.
But, when I try this:
public class Test {
public static void main (String[] args) {
String input = JOptionPane.showInputDialog(null, "Make your choice!\n" +
"1. load file");
load();
}
}
the window is not showing however, the programming is still running... I have no clue what might be causing this problem
Java on the Mac is really picky about Swing things only occurring in the Event Dispatch Thread. Try this.
import java.awt.EventQueue;
import javax.swing.JFileChooser;
public class Test {
private static int result;
public static void main(String[] args) throws Exception {
EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
String folder = System.getProperty("user.dir");
JFileChooser fc = new JFileChooser(folder);
result = fc.showOpenDialog(null);
}
});
System.out.println(result);
}
}
Documentation for InvokeAndWait is here. But basically, you pass it a Runnable that does Swing stuff, and it will execute that in the right thread. There's also InvokeLater if you don't want to wait.