java - 取消文件打开对话框后出现 InterruptedException - 1.6.0_26

标签 java swing jfilechooser interrupted-exception

以下代码的输出是:

java.vendor     Sun Microsystems Inc.
java.version    1.6.0_26
java.runtime.version    1.6.0_26-b03
sun.arch.data.model     32
os.name     Windows XP
os.version  5.1
os.arch     x86
Input selection cancelled by user.
Exception while removing reference: java.lang.InterruptedException
java.lang.InterruptedException
    at java.lang.Object.wait(Native Method)
    at java.lang.ref.ReferenceQueue.remove(Unknown Source)
    at java.lang.ref.ReferenceQueue.remove(Unknown Source)
    at sun.java2d.Disposer.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

以下代码显示了我机器上的异常。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GUI extends JPanel implements ActionListener {

    private final String newline = System.getProperty("line.separator");
    JButton openButton;
    JTextArea log;
    JFileChooser fc;

    public GUI() {
        super(new BorderLayout());

        log = new JTextArea(20,40);
        log.setMargin(new Insets(5,5,5,5));
        log.setEditable(false);

        fc = new JFileChooser();

        openButton = new JButton("Open");
        openButton.addActionListener(this);

        JPanel buttonPanel = new JPanel(); //use FlowLayout
        buttonPanel.add(openButton);

        add(buttonPanel, BorderLayout.NORTH);
        add(new JScrollPane(log));

        showProp("java.vendor");
        showProp("java.version");
        showProp("java.runtime.version");
        showProp("sun.arch.data.model");
        showProp("os.name");
        showProp("os.version");
        showProp("os.arch");
    }

    public void showProp(String name) {
        output(name + " \t" + System.getProperty(name));
    }

    public void output(String msg) {
        log.append(msg + newline);
        log.setCaretPosition(log.getDocument().getLength());
        System.out.println(msg);
    }

    public void actionPerformed(ActionEvent e) {
        //Handle open button action.
        int returnVal = fc.showOpenDialog(GUI.this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            //This is where a real application would open the file.
            output(
                "Input File Selected: " +
                fc.getSelectedFile().getName() +
                ".");

        } else {
            output("Input selection cancelled by user.");
        }
        log.setCaretPosition(log.getDocument().getLength());
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("IDE Output Converter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add content to the window.
        frame.add(new GUI());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

当我运行程序时,主窗口打开正常,程序运行良好。

但是,如果您:

  • 使用“打开文件”按钮打开 JFileChooser
  • 按取消,然后
  • 退出程序

抛出一个InterruptedException。或者,如果您选择一个文件并“打开”它,然后退出程序,则会抛出相同的错误。关于这个blog用示例代码解释了同样的事情,他的解决方案是尽快调用 new JFileChooser(); ,我这样做没有效果。

这是 1.6.0_26 中的错误吗?如果是这样,该版本是否有解决方法?

是代码吗?如果是这样,如何解决? (看起来不太可能,还有 2 个其他空结果 - 其中一个现在已删除。)

最佳答案

我会说这是 sun.awt.Disposer 中的一个小错误。

该类创建“Java2D Disposer”守护线程,处理垃圾收集对象(主要是 AWT 窗口)的 AWT 资源处理。大多数情况下,该线程在其引用队列上等待一个新的一次性对象被垃圾收集。当线程被中断时,它会显式打印该异常。

当 JVM 终止时,它会中断所有线程。在某些情况下 - 这显然受到 JFileChooser 的使用和由它初始化的子系统的影响 - 一些线程在此中断后仍然有机会运行。在这种情况下,InterruptedException 在“Java2D Disposer”线程中被抛出,因为它正在等待锁。如果它在关闭期间忽略该异常会更好。

作为解决方法,替换

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosed(WindowEvent e) {
        PrintStream nullStream = new PrintStream(new OutputStream() {
            public void write(int b) throws IOException {
            }

            public void write(byte b[]) throws IOException {
            }

            public void write(byte b[], int off, int len) throws IOException {
            }
        });
        System.setErr(nullStream);
        System.setOut(nullStream);
        System.exit(0);
    }
});

关于java - 取消文件打开对话框后出现 InterruptedException - 1.6.0_26,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7769885/

相关文章:

java - 使用 ScheduledExecutorService,如何启动一个线程而不等待其他线程以固定时间间隔完成?

java - 为什么我的 JavaFX 应用程序中没有显示任何内容?

java - 矩形网格内的三角形

java - FilenameUtils.getExtension 比较返回 false

java - 如何存储图像并在存储时同时重命名该图像?

java - 如何在机器人框架中使用 "Choose File"关键字上传文件

java - Rally Java API 仅从项目中拉取团队成员

java - 将对象类型作为参数传递给方法?

java - 从 netbeans 的 main 方法中操作 jLabel

java - 实现 keylistener 和 jpanel 的单独类