我试图覆盖process()
方法以确保某些代码在EDT中运行。我确保<T,V>
始终匹配,但它仍然不会让我覆盖。这是我的代码:
final SwingWorker<ArrayList<Block>,Integer[]> swingSlaveLabourer = new SwingWorker<ArrayList<Block>, Integer[]>() {
@Override
protected ArrayList<Block> doInBackground() throws Exception {
blockList.doHeavyWork()..
Integer [] status = new Integer[2];
status[0] = 1;
status[1] = 0;
this.process(status);
return blockList;
}
@Override //wont allow override
protected void process (Integer[] chunks){
progressBar.setValue(chunks[0]);
}
};
答案 0 :(得分:2)
process
方法以List<V>
为参数。它表示已发布的所有数据块。您需要将代码更改为:
@Override
protected void process(List<Integer[]> chunks) {
// do something
}
您不应直接从process
方法调用doInBackground
。相反,您应该调用publish
来发布每个数据块。
请参阅以下来自SwingWorker
Javadoc的示例:
class PrimeNumbersTask extends SwingWorker<List<Integer>, Integer> {
PrimeNumbersTask(JTextArea textArea, int numbersToFind) {
//initialize
}
@Override
public List<Integer> doInBackground() {
while (! enough && ! isCancelled()) {
number = nextPrimeNumber();
publish(number); // each computed number is published
setProgress(100 * numbers.size() / numbersToFind); // and progress updated
}
}
return numbers;
}
@Override
protected void process(List<Integer> chunks) {
// in the mean while, we print each published value to a textarea
for (int number : chunks) {
textArea.append(number + "\n");
}
}
}