我喜欢使用SwingWorker来处理花费很多时间的任务,但我发现它使我的代码难以阅读。例如:
我之前:
classCombo.setModel(Controller.getAllClasses());
现在:
private void downloadData()
{
final WaitDialog waitDialog = new WaitDialog(this);
SwingWorker<Object, Void> sw = new SwingWorker<Object, Void>()
{
@Override
protected Object doInBackground() throws Exception
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
waitDialog.start(null);
}
});
return Controller.getAllClasses();
}
@Override
protected void done()
{
Vector<KlasaEntity> klasy = null;
try
{
klasy = (Vector<KlasaEntity>) get();
waitDialog.stop();
}
catch (Exception e)
{
e.printStackTrace();
}
classCombo.setModel(new DefaultComboBoxModel<KlasaEntity>(klasy));
}
};
sw.execute();
你认为有什么机会缩短这段代码吗?