I am using Eclipse to develop an SWT application. The following code works on Windows but not on Macintosh:
import javax.swing.JFileChooser;
public class Test {
public static void main(String[] args) {
final JFileChooser fc = new JFileChooser();
int ret = fc.showOpenDialog(null);
System.out.println("ret = " + ret);
}
}
Upon entering showOpenDialog
, the Mac cursor spins forever, and I get the following in the Java console:
2013-09-05 08:20:40.568 java[1271:707] [Java CocoaComponent compatibility mode]: Enabled
2013-09-05 08:20:40.569 java[1271:707] [Java CocoaComponent compatibility mode]: Setting timeout for SWT to 0.100000
2013-09-05 08:20:41.227 java[1271:dd03] *** -[NSConditionLock unlock]: lock (<NSConditionLock: 0x7fa211e82600> '(null)') unlocked when not locked
2013-09-05 08:20:41.227 java[1271:dd03] *** Break on _NSLockError() to debug.
I've tried Java 1.6, Java 1.7. I've tried setting
-Dcom.apple.awt.CocoaComponent.CompatibilityMode=false -XstartOnFirstThread
but that has no effect.
This must be something really basic. What am I missing?
Good day to everybody having same problem!
Maybe i am too late to answer this but it might help someone having this issue.
After some research i have tries to play around with LookAndFeel. Then i tried changing look and feel upon opening "showSaveDialog()" and it seems to work. I cannot guarantee that it works 100% of the time, but till now it has worked to me just fine("did not succeed to hang :)"). Ill report again if it fails :) Here is my code:
//Update: It it better to user FileDialogg for mac os x
private File saveFile() {
String osName = System.getProperty("os.name");
String homeDir = System.getProperty("user.home");
File selectedPath = null;
if (osName.equals("Mac OS X")) {
System.setProperty("apple.awt.fileDialogForDirectories", "true");
FileDialog fd = new FileDialog(f, "Choose a file", FileDialog.LOAD);
fd.setDirectory(homeDir);
fd.setVisible(true);
String filename = fd.getDirectory();
selectedPath = new File(filename);
if (filename == null) {
System.out.println("You cancelled the choice");
} else {
System.out.println("You chose " + filename);
}
System.setProperty("apple.awt.fileDialogForDirectories", "true");
} else {
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.setCurrentDirectory(new File(homeDir));
fc.setAcceptAllFileFilterUsed(false);
fc.showOpenDialog(null);
selectedPath = fc.getSelectedFile();
}
return selectedPath;
}
Code is not perfect but u get the point :)
This program runs fine on my Mac and returns in less than a second:
import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.*;
/** to isolate and understand why JFileChooser is blocking. */
public class DebugJFC {
public static void main(String[] args) {
System.err.println("JFileChooser ");
JFileChooser listFC= new JFileChooser(".");
System.err.println("done");
}
}
When I run it on Linux, it hangs after printing "JFileChooser " and before printing "done".
Worse yet, the "new JFileChooser" statement has worked for years on Linux, and only started failing today. What's up with that!??
Linux: > java -version
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
Mac: > java -version
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)