简体   繁体   English

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

[英]Why does JfileChooser.showOpenDialog hang on Mac OSX?

I am using Eclipse to develop an SWT application. 我正在使用Eclipse开发SWT应用程序。 The following code works on Windows but not on Macintosh: 以下代码在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);
    }
}

Upon entering showOpenDialog , the Mac cursor spins forever, and I get the following in the Java console: 进入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 (<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. 我尝试过Java 1.6,Java 1.7。 I've tried setting -Dcom.apple.awt.CocoaComponent.CompatibilityMode=false -XstartOnFirstThread but that has no effect. 我尝试设置-Dcom.apple.awt.CocoaComponent.CompatibilityMode=false -XstartOnFirstThread但这没有任何效果。

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. 经过研究后,我尝试使用LookAndFeel。 Then i tried changing look and feel upon opening "showSaveDialog()" and it seems to work. 然后,我尝试在打开“ showSaveDialog()”时更改外观,感觉似乎可行。 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 :)"). 我不能保证它会100%地起作用,但是到现在为止,它对我来说还不错(“未成功挂起:)”)。 Ill report again if it fails :) Here is my code: 如果失败,请再次报告错误:)这是我的代码:

//Update: It it better to user FileDialogg for mac os x //更新:最好在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;
}

Code is not perfect but u get the point :) 代码不是完美的,但你明白了:)

This program runs fine on my Mac and returns in less than a second: 该程序可以在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");
        }

    }

When I run it on Linux, it hangs after printing "JFileChooser " and before printing "done". 当我在Linux上运行它时,它在打印“ JFileChooser”之后并在打印“ done”之前挂起。 Worse yet, the "new JFileChooser" statement has worked for years on Linux, and only started failing today. 更糟糕的是,“ new JFileChooser”语句在Linux上已经工作了多年,直到今天才开始失败。 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) 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 Runtime Environment (build 1.6.0_65-b14-462-11M4609) Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode) Mac:> java -version Java版本“ 1.6.0_65” Java SE运行时环境(内部版本1.6.0_65-b14-462-11M4609)Java HotSpot(TM)64位服务器VM(内部版本20.65-b04-462,混合模式)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM