为什么JfileChooser.showOpenDialog在Mac OSX上挂起?

我正在使用Eclipse开发SWT应用程序。 以下代码适用于Windows但不适用于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); } } 

进入showOpenDialog ,Mac游标将永远旋转,我在Java控制台中获得以下内容:

 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 ( '(null)') unlocked when not locked 2013-09-05 08:20:41.227 java[1271:dd03] *** Break on _NSLockError() to debug. 

我尝试过Java 1.6,Java 1.7。 我已经尝试设置-Dcom.apple.awt.CocoaComponent.CompatibilityMode=false -XstartOnFirstThread但是没有效果。

这必须是非常基本的东西。 我错过了什么?

有相同问题的每个人的好日子!

也许我来不及回答这个问题,但它可能会帮助有这个问题的人。

经过一些研究,我试图玩LookAndFeel。 然后我尝试在打开“showSaveDialog()”时改变外观和感觉,它似乎工作。 我不能保证它在100%的时间都能正常工作,但到目前为止它对我很有用(“没有成功挂起:)”)。 如果失败,我会再次报告:)这是我的代码:

//更新:对于mac os x,用户FileDialogg更好

 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; } 

代码不完美,但你明白了:)

这个程序在我的Mac上正常运行,并在不到一秒的时间内返回:

 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"); } } 

当我在Linux上运行它时,它会在打印“JFileChooser”之后和打印“完成”之前挂起。 更糟糕的是,“新的JFileChooser”声明已在Linux上运行多年,并且今天才开始失败。 那是怎么回事!??

Linux:> java -version java版“1.7.0_45”Java(TM)SE运行时环境(版本1.7.0_45-b18)Java HotSpot(TM)64位服务器VM(版本24.45-b08,混合模式)

Mac:> java -version java version“1.6.0_65”Java(TM)SE运行时环境(版本1.6.0_65-b14-462-11M4609)Java HotSpot(TM)64位服务器VM(版本20.65-b04-462,混合模式)