我一直在努力熟悉 Java 中 SwingWorkers 的使用。是否可以在 SwingWorker 的处理方法中从 GUI 触发显示对话框?
最佳答案
within a SwingWorker's process method
如果你的意思是:
a) 在 doInBackground() 内,
public Boolean doInBackground() {
// caught exception half way e.g. login auth fail
// 'push' into GUI layer (EDT)
String text = "Your error here";
publish(text);
Boolean result = Boolean.TRUE;
// continue with remaining process
return result;
}
protected void process(List<Object> chunks) {
String[] message = chunks.toArray(new String[chunks.size()]);
// prompt user with dialog box
JDialogBox.showMessageDialog(window, message);
}
<小时/>
b) 在done()内,
protected void done() {
// prompt user with dialog box
String message;
try {
message = get();
} catch (Exception e) {
e.printStackTrace();
}
JDialogBox.showMessageDialog(window, message);
}
<小时/>
c) 还是在 get() 之外?
SwingWorker sw = new SwingWorker() {.....;
sw.execute();
// some time later
final String message;
try {
message = sw.get();
} catch (Exception e) {
e.printStackTrace();
}
Runnable edt = new Runnable() {
public void run() {
JDialogBox.showMessageDialog(window, message);
}
}
java.awt.EventQueue.invokeLater(edt);
关于java - 如何使用 SwingWorker(外部类)触发对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6631293/