import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class ProgressBarInTableDemo extends JPanel {
private BarModel model = new BarModel();
private JTable tbl = new JTable(model);
private JScrollPane jsp = new JScrollPane();
private JButton btn = new JButton("Add Task");
private static final int BAR_WIDTH = 200;
private static final int BAR_HEIGHT = 40;
public ProgressBarInTableDemo() {
super(new BorderLayout());
initComponents();
}
private void initComponents() {
tbl.setRowHeight(BAR_HEIGHT);
TableColumn barColumn = tbl.getColumnModel().getColumn(1);
barColumn.setCellRenderer(new BarRenderer());
barColumn.setMinWidth(BAR_WIDTH);
barColumn.setMaxWidth(BAR_WIDTH);
jsp.setViewportView(tbl);
add(jsp, BorderLayout.CENTER);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Task(String.format("%d", e.getWhen()), e.getWhen() % 2 == 0).start();
}
});
add(btn, BorderLayout.NORTH);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("BarTable");
frame.setContentPane(new ProgressBarInTableDemo());
frame.setMinimumSize(new Dimension(600, 400));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
class Task extends Thread {
private String taskName;
private boolean nothing;
private int index;
public Task(String taskName, boolean nothing) {
this.taskName = taskName;
this.nothing = nothing;
}
@Override
public void run() {
final Integer barValue = new Integer(0);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
model.addTask(taskName, barValue, nothing);
}
});
for (index = 0; index < 100; index++) {
try {
Thread.sleep((long) (Math.random() * Math.random() * 1000 + 1));
} catch (InterruptedException ex) {
Logger.getLogger(ProgressBarInTableDemo.class.getName()).log(Level.SEVERE, null, ex);
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
model.updateBarValue(taskName, new Integer(index));
}
});
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
model.removeTask(taskName);
}
});
}
}
}
class BarRenderer extends JProgressBar implements TableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setStringPainted(true);
setValue(((Integer) value).intValue());
return this;
}
}
class BarModel extends DefaultTableModel {
private static String[] columnNames = {"Task Name", "ProgressBar", "Nothing"};
public BarModel(){
super(columnNames, 0);
}
public void addTask(String taskName, Integer barValue, boolean xx) {
int row = 0;
while (row < getRowCount()) {
if (taskName.equals(getValueAt(row, 0))) {
return;
}
row++;
}
Object[] rowData = {taskName, barValue, xx};
addRow(rowData);
}
public void updateBarValue(String taskName, Integer newValue) {
int row = 0;
while (row < getRowCount()) {
if (taskName.equals((String) getValueAt(row, 0))) {
setValueAt(newValue, row, 1);//0:taskname 1: progressbar
break;
}
row++;
}
}
public void removeTask(String taskName) {
int row = 0;
while (row < getRowCount()) {
if (taskName.equals((String) getValueAt(row, 0))) {
removeRow(row);
break;
}
row++;
}
}
public Class getClass(int columnIndex) {
return getValueAt(0, columnIndex).getClass();
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
if (columnIndex == 2) {
return true;
}
return false;
}
}