我正在开发一个具有jtable的应用程序,用户可以开始下载。我想在表中添加下载名称,状态和进度条。我这样做:
INIT JTABLE :( NETBEANS自动创建代码(有没有办法编辑它?o.O))
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null},
{null, null, null},
{null, null, null},
{null, null, null}
},
new String [] {
"Categoria", "Sottocategoria", "Progresso (%)"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class, java.lang.Object.class
};
boolean[] canEdit = new boolean [] {
false, false, false
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jTable1.setColumnSelectionAllowed(true);
jScrollPane2.setViewportView(jTable1);
jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
创建RENDERER:
class ProgRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
return (JProgressBar) value;
}
}
将其添加到最后一个COLOUM:
jTable1.getColumn("Progresso (%)").setCellRenderer(new ProgRenderer());
现在我创建了一个ArrayList<Thread>
来保存活动线程,还有一个ArrayList<JProgressBar>
来保存jtable中存在的所有进度条(正确的方式?)。
现在添加新行,我这样做:
DefaultTableModel d = (DefaultTableModel) jTable1.getModel();
work++; // to have a number of active threads.
progress.add(getProgress(x, total));
d.addRow(new Object[]{Html.categoria, "Inizio scansione", progress.get(work)});
Thread t = new Thread(new Runnable() {
@Override
public void run() {
int max = Html.totale;
for(int i=1;i<=max;i++){
try {
progress.get(work).setValue(i); // JUST FOR TRY
System.out.println(progress.get(work).getValue()); // TRY
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
}
});
working.add(t);
t.start();
private JProgressBar getProgress(int x, int total) {
JProgressBar progressCsv = new JProgressBar();
progressCsv.setMaximum(total);
progressCsv.setStringPainted(true);
progressCsv.setString("0%");
return progressCsv;
}
我的问题是进度没有更新,我的方法是错的?你能帮助我吗? thanksss
答案 0 :(得分:0)
我建议你:
在表格中仅保留有关进度的信息。
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class, java.lang.Integer.class
};
实施进度条以扩展JProgressBar
class ProgRenderer extends JProgressBar implements
TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
this.setValue((Integer)value)
return this;
}
}
增量值
@Override
public void run() {
int max = Html.totale;
for(int i=1;i<=max;i++){
try {
SwingUtiliites.invokeLater(new Runnable(){
public void run(){
jTable1.setValueAt(Integer.valueOf(i),row,2);
}
}
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
}