有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Swing丢失异常

当我启动模态对话框时,当它出现渲染异常时,该异常会出现。我可以在调用对话框的代码中捕获它吗。setVisible()

另外,我知道线程。setUncaughtExceptionHandler,我需要从调用该对话框的代码中捕获它

请提前通知,谢谢,安德烈

public class TestSwing {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    JDialog jDialog = new JDialog();
                    JTable table = new JTable();
                    table.setModel(new DefaultTableModel() {
                        @Override public int getColumnCount() {return 1;}
                        @Override public int getRowCount() {return 1;}
                        @Override
                        public Object getValueAt(int row, int column) {
                            throw new RuntimeException("Hello");
                        }
                    });

                    jDialog.add(table);
                    jDialog.setModal(true);
                    jDialog.pack();
                    jDialog.setVisible(true);

                    System.out.println("dialog closed");

                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("got it");
               }

           }
        });
    }
}

共 (1) 个答案

  1. # 1 楼答案

    不能,因为异常不在对话框中,而是在线程中。对话setVisible()会等待对话框变为不可见,但不要停止当前线程。但我有一个小诀窍,它可能会帮助你获得想要的行为

    public class TestSwing {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        JDialog jDialog = new JDialog();
                        JTable table = new JTable();
                        table.setModel(new DefaultTableModel() {
                            @Override public int getColumnCount() {return 1;}
                            @Override public int getRowCount() {return 1;}
                            @Override
                            public Object getValueAt(int row, int column) {
                                throw new RuntimeException("Hello");
                            }
                        });
    
                        jDialog.add(table);
                        jDialog.setModal(true);
                        jDialog.pack();
                        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    
                            @Override
                            public void uncaughtException(Thread t, Throwable e) {
                                jDialog.setVisible(false);
                                Thread.setDefaultUncaughtExceptionHandler(null);
                                throw new RuntimeException(e);
                            }
                        });
                        jDialog.setVisible(true);
    
                        System.out.println("dialog closed");
    
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println("got it");
                   }
    
               }
            });
        }
    }