我有一个摇摆工,所以我在后台工作时我的GUI可以保持响应.
问题是我需要在各个点更新gui(例如,使用程序状态/信息更新标签),但是我有很多处理要做,而我无法通过一个巨大的doInBackground方法来完成所有工作.
所以我从doInBackground方法实例化其他类来做到这一点,问题是我无法更新gui,除非在此SW线程或EDT中.
注意:为简便起见,我删除了不必要的代码
我希望能够从DoWork1&中更新gui. DoWork2等.我尝试过传递对SW swingworker对象的引用,但是大概不能访问publish方法,因为它受到了保护.我也许可以使用反射来更新gui,但是任何使用我的代码的人都不会喜欢.我确实听到有人提到过使用反向接口,但是没有示例,因此我对此一无所知.
任何帮助,将不胜感激,谢谢.
public class SW extends SwingWorker {
private DoWork1 doWork1;
private DoWork2 doWork2;
private Iframe frame;
public SW(Iframe frame, DoWork1 doWork1) {
this.frame = frame;
this.doWork1 = doWork1;
this.doWork2 = new DoWork2();
}
@Override
protected Object doInBackground() {
doWork1.lotsOfWork("");
publish("Can write to label from here");
doWork2.lotsOfWork("", "");
return null;
}
@Override
public void process(List chunks) {
for (String s : chunks) {
frame.getLabel().setText(s);
}
}
@Override
protected final void done() {
}
}
public class DoWork1 {
public DoWork1() {
}
private lotsOfWork(String s) {
//Do lots of work in here
//But need to update the gui with information!
//would like to be able to do something like:
publish("somehow write to label from here!?");
}
}
public class DoWork2 {
public DoWork2() {
}
private lotsOfWork(String s, String s) {
//Do lots more work in here
//But need to update the gui with information!
//would like to be able to do something like:
publish("somehow write to label from here!?");
}
}