【问题标题】:JFileChooser.showOpenDialog() freezing my programJFileChooser.showOpenDialog() 冻结我的程序
【发布时间】:2014-05-13 23:15:20
【问题描述】:

JFileChooser 类有问题。我正在使用以下类(我确实编写过)来一个接一个地加载几个文件,它通常适用于 2 或 3 个文件(有时 1,有时 6,即使它不一定是随机的)并且在某一点上,它在showOpenDialog(null), 冻结,没有抛出异常,也没有返回任何内容。 我真的不知道它来自哪里。

这是我的课:

public class CustomFileChooser extends JFileChooser {

    public File chooseFile(String windowTitle, String description, String extension, boolean mustExist) {

        setDialogTitle(windowTitle);
        resetChoosableFileFilters();
        setAcceptAllFileFilterUsed(false);
        addChoosableFileFilter(new CustomFileFilter(description, new String[] {extension}));
        setSelectedFile(new File(""));

        if (mustExist) {
            setApproveButtonText("Open");
        } else {
            setApproveButtonText("Save");
        }

        File file = null;
        while (file == null) {

            if (showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {

                file = getSelectedFile();
                if (mustExist) {
                    if (!file.canRead()) {
                        file = null;
                        JOptionPane.showMessageDialog(null, "Cannot read from the specified file!", "Error while opening the file", JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    if (!file.getName().toLowerCase().endsWith(extension.toLowerCase())) {
                        file = new File(file.getAbsolutePath().concat(extension));
                    }
                    if (file.exists()) {
                        if (file.canWrite()) {
                            if (JOptionPane.showConfirmDialog(null, "Do you really want to overwrite this file?", "Erasing file", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.NO_OPTION) {
                                file = null;
                            }
                        } else {
                            file = null;
                            JOptionPane.showMessageDialog(null, "Cannot write to the specified file!", "Error while opening the file", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }
            } else {
                return null;
            }
        }

        return file;
    }

    private static final long serialVersionUID = 1L;
}

编辑:我尝试在 Windows 上运行我的程序,一切正常。您是否了解有关此类/方法的平台相关问题?

【问题讨论】:

  • showOpenDialog在什么类?我假设 JFileChooser 但我想确定
  • 如果您在 OSX 上遇到此问题,这个问题可能会有所帮助:stackoverflow.com/questions/18642158/…(阅读答案评论)
  • @Caleb 是的,showOpenDialog 继承自 JFileChooser 类。在发布这个问题之前,我已经看过这个问题,尽管我不确定我是否真的理解是这个人的问题。但据我所知,我没有在另一台机器上运行任何服务器或任何程序。如果我有什么问题,请告诉我。
  • 在同一个问题中运行,有一个在另一个项目中工作的最小示例。无法让它在我当前的项目中工作。

标签: java swing file-io jfilechooser


【解决方案1】:

在代码块中使用您的代码,如下所示。

private void fileChooserMethod() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
           javax.swing.JFileChooser fc_file_selector= new JFileChooser();
           int response =  fc_file_selector.showOpenDialog(null);
           //your code here
        }
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    相关资源
    最近更新 更多