My Swing application has a table which is populated when some data are fetched from an external source. To display data I'm using SwingWorker thread which is called from the thread that is supposed to fetch the data in this way:
// SwingWorker Thread
private class DisplayRowsTask extends SwingWorker<Void, Object[]> {
private Vector<String[]> _data;
@Override
protected Void doInBackground() {
while (_continue && !hasError()) {
// some logic to fetch and place data in _data
// data adjustment
for (String[] row : _data) publish (row);
_data = new Vector<String[]>();
}
return null;
}
@Override
protected void process(List<Object[]> rows) {
for (Object[] row : rows) ((DefaultTableModel)_table.getModel()).addRow(row);
}
}
The results displayed are correct and everything works fine apart that the CPU usage is 100% (and over!) especially in the situation where a lot of data are fetched frequently. How can I limit this usage?
Another question maybe not fully related: how can I know if the data are been displayed in the table? The method "SwingWorker.isDone()" doesn't give this information