1 回答
TA贡献1784条经验 获得超9个赞
不,您不能,因为异常不在您的对话框中,而是在线程中。Dialog.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");
}
}
});
}
}
添加回答
举报