Code
private final JTable table = new JTable(model) {
@Override public void updateUI() {
super.updateUI();
removeColumn(getColumnModel().getColumn(3));
JProgressBar progress = new JProgressBar();
TableCellRenderer renderer = new DefaultTableCellRenderer();
TableColumn tc = getColumnModel().getColumn(2);
tc.setCellRenderer((tbl, value, isSelected, hasFocus, row, column) -> {
Component c;
if (value instanceof JProgressBar) {
c = (JProgressBar) value;
} else if (value instanceof Integer) {
progress.setValue((int) value);
c = progress;
} else {
c = renderer.getTableCellRendererComponent(
tbl, Objects.toString(value), isSelected, hasFocus, row, column);
}
return c;
});
}
};
class IndeterminateProgressBarUI extends BasicProgressBarUI {
@Override public void incrementAnimationIndex() {
super.incrementAnimationIndex();
}
}
class BackgroundTask extends SwingWorker<Integer, Object> {
private final Random rnd = new Random();
@Override protected Integer doInBackground() throws InterruptedException {
int lengthOfTask = calculateTaskSize();
int current = 0;
int total = 0;
while (current <= lengthOfTask && !isCancelled()) {
publish(100 * current / lengthOfTask);
total += doSomething();
current++;
}
return total;
}
private int calculateTaskSize() throws InterruptedException {
int total = 0;
JProgressBar indeterminate = new JProgressBar() {
@Override public void updateUI() {
super.updateUI();
setUI(new IndeterminateProgressBarUI());
}
};
indeterminate.setIndeterminate(true);
// Indeterminate loop:
for (int i = 0; i < 200; i++) {
int iv = rnd.nextInt(50) + 1;
Thread.sleep(iv);
ProgressBarUI ui = indeterminate.getUI()
((IndeterminateProgressBarUI) ui).incrementAnimationIndex();
publish(indeterminate);
total += iv;
}
return 1 + total / 100;
}
private int doSomething() throws InterruptedException {
int iv = rnd.nextInt(50) + 1;
Thread.sleep(iv);
return iv;
}
}
References
No comments:
Post a Comment