java-SwingWorker,未调用done()方法
作者:互联网
这只是SwingWorker的一个实现:
class GuiWorker extends SwingWorker<Integer, Integer> {
private JFrame frame = new JFrame();
private JDialog dialog = new JDialog(frame, "Loadin data", true);
private JProgressBar progressBar = new JProgressBar();
private Statistics st = new Statistics();
public GuiWorker(GraphEditor editor, Statistics st) {
this.st = st;
Window mainWindow = SwingUtilities.windowForComponent(editor
.getGraphComponent().getParent());
dialog.setSize(400, 200);
int x = mainWindow.getX() + (mainWindow.getWidth() - dialog.getWidth())
/ 2;
int y = mainWindow.getY()
+ (mainWindow.getHeight() - dialog.getHeight()) / 2;
progressBar.setString("Have fun to wait some time...");
progressBar.setStringPainted(true);
progressBar.setIndeterminate(true);
dialog.add(progressBar);
dialog.setModal(true);
dialog.setLocation(x, y);
dialog.setVisible(true);
}
@Override
protected Integer doInBackground() throws Exception {
st.loadInitialData();
return 0;
}
@Override
protected void done() {
dialog.setVisible(false);
JLabel label = new JLabel("Task Complete");
dialog.getContentPane().remove(progressBar);
dialog.getContentPane().add(label);
dialog.getContentPane().validate();
dialog.setVisible(false);
}
}
诊断日志永远不会隐藏的问题,直到我强行关闭它(任务完成后必须隐藏).
我注意到loadInitialData()方法是一种从我的数据库中收集一些统计信息的方法,这需要花费几秒钟的时间.
更新:我确定仅在关闭对话框时才会调用done()方法.
更新:
我在使用GuiWorker的位置在这里:
mainTabs.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
switch (mainTabs.getSelectedIndex()) {
case 0:
case 1:
case 2: // stats tab
GuiWorker gw = new GuiWorker(editor,st);
gw.execute();
break;
default:
break;
}
}
});
解决方法:
所有的摆动事件都在Event Dispatch Thread内部运行.因此,添加到mainTab中的ChangeListener在事件调度线程中运行.
在事件侦听器中,您将实例化GuiWorker,并在该构造函数中通过调用dialog.setVisible(true);打开对话框.
您的JDialog实例是模态的,在模态对话框上调用setVislbe(true)将阻塞调用线程,在您的情况下为事件调度线程.因此,在关闭对话框之前,请执行gw.execute();.不被调用.这就是为什么在关闭对话框之前不会调用您的done方法.
为了使代码正常工作,可以尝试不要在构造函数中调用setVisible(true),而是从GuiWorker提供一种方法来设置对话框的可见性.然后在调用gw.execute();之后;呼叫gw.setVisible(true).
为了澄清,尝试
class GuiWorker extends SwingWorker<Integer, Integer> {
private JFrame frame = new JFrame();
private JDialog dialog = new JDialog(frame, "Loadin data", true);
private JProgressBar progressBar = new JProgressBar();
private Statistics st = new Statistics();
public GuiWorker(GraphEditor editor, Statistics st) {
this.st = st;
Window mainWindow = SwingUtilities.windowForComponent(editor
.getGraphComponent().getParent());
dialog.setSize(400, 200);
int x = mainWindow.getX() + (mainWindow.getWidth() - dialog.getWidth())
/ 2;
int y = mainWindow.getY()
+ (mainWindow.getHeight() - dialog.getHeight()) / 2;
progressBar.setString("Have fun to wait some time...");
progressBar.setStringPainted(true);
progressBar.setIndeterminate(true);
dialog.add(progressBar);
dialog.setModal(true);
dialog.setLocation(x, y);
}
@Override
protected Integer doInBackground() throws Exception {
st.loadInitialData();
return 0;
}
@Override
protected void done() {
dialog.setVisible(false);
JLabel label = new JLabel("Task Complete");
dialog.getContentPane().remove(progressBar);
dialog.getContentPane().add(label);
dialog.getContentPane().validate();
dialog.setVisible(false);
}
public void setVisible(boolean visible) {
dialog.setVisible(visible);
}
}
和
mainTabs.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
switch (mainTabs.getSelectedIndex()) {
case 0:
case 1:
case 2: // stats tab
GuiWorker gw = new GuiWorker(editor,st);
gw.execute();
gw.setVisible(true);
break;
default:
break;
}
}
});
标签:swingworker,multithreading,swing,java 来源: https://codeday.me/bug/20191029/1961485.html