且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-01-30 21:33:47

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



也许我太晚了,不能回答这个问题,但可能会帮助有人遇到这个问题。



经过一些研究,我试图用LookAndFeel玩。然后我尝试改变外观和感觉打开showSaveDialog(),它似乎工作。我不能保证它工作100%的时间,但到现在它已经对我很好(没有成功挂:))。再次报错如果它失败:)这里是我的代码:



//更新:它更好的用户的FileDialogg为mac os x

 私人文件saveFile(){
String osName = System.getProperty(os.name);
String homeDir = System.getProperty(user.home);
文件selectedPath = null;
if(osName.equals(Mac OS X)){
System.setProperty(apple.awt.fileDialogForDirectories,true);
FileDialog fd = new FileDialog(f,选择文件,FileDialog.LOAD);
fd.setDirectory(homeDir);
fd.setVisible(true);
String filename = fd.getDirectory();
selectedPath = new File(filename);
if(filename == null){
System.out.println(你取消了选择);
} else {
System.out.println(你选择了+ 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;
}

代码不完美,但你得到点:)


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 :)