我们在多文件下载或多事务处理时,经常会出现使用线程以提高效率的情况,而这时在GUI程序中如何表示进度,就成了一个不大不小的问题。
现在比较被大众接受的方式,大体就是如迅雷等下载工具中一样,用表格中加载进度条以进行显示。
而对于Swing来说,并没有现成的组件能够实现这一操作,还有下载的并发,似乎也需要额外进行处理。于是,我在此提供一个基于jdk1.6版本的示例,以供参考。(因为jdk1.6提供了SwingWorker,简化了图形程序中的线程处理,使用其他jdk开发请替换此项即可)
本示例由两个java文件组成
MyTableModel.java
现在比较被大众接受的方式,大体就是如迅雷等下载工具中一样,用表格中加载进度条以进行显示。
而对于Swing来说,并没有现成的组件能够实现这一操作,还有下载的并发,似乎也需要额外进行处理。于是,我在此提供一个基于jdk1.6版本的示例,以供参考。(因为jdk1.6提供了SwingWorker,简化了图形程序中的线程处理,使用其他jdk开发请替换此项即可)
本示例由两个java文件组成
MyTableModel.java
package
org.loon.test;
import java.awt.Component;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.SwingWorker;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
/**
* <p>
* Title: LoonFramework
* </p>
* <p>
* Description:
* </p>
* <p>
* Copyright: Copyright (c) 2007
* </p>
* <p>
* Company: LoonFramework
* </p>
*
* @author chenpeng
* @email:ceponline@yahoo.com.cn
* @version 0.1
*/
public class MyTableModel extends DefaultTableModel {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final ColumnContext[] columnArray = {
new ColumnContext("ID", Integer.class, false),
new ColumnContext("名称", String.class, false),
new ColumnContext("进度", Integer.class, false) };
private final Map<Integer, SwingWorker> swmap = new HashMap<Integer, SwingWorker>();
private int number = 0;
public void addTest(Test t, SwingWorker worker) {
Object[] obj = { new Integer(number), t.getName(), t.getProgress() };
super.addRow(obj);
swmap.put(number, worker);
number++;
}
public synchronized SwingWorker getSwingWorker(int identifier) {
Integer key = (Integer) getValueAt(identifier, 0);
return swmap.get(key);
}
public Test getTest(int identifier) {
return new Test((String) getValueAt(identifier, 1),
(Integer) getValueAt(identifier, 2));
}
public boolean isCellEditable(int row, int col) {
return columnArray[col].isEditable;
}
public Class<?> getColumnClass(int modelIndex) {
return columnArray[modelIndex].columnClass;
}
public int getColumnCount() {
return columnArray.length;
}
public String getColumnName(int modelIndex) {
return columnArray[modelIndex].columnName;
}
private static class ColumnContext {
public final String columnName;
public final Class columnClass;
public final boolean isEditable;
public ColumnContext(String columnName, Class columnClass,
boolean isEditable) {
this.columnName = columnName;
this.columnClass = columnClass;
this.isEditable = isEditable;
}
}
}
class Test {
import java.awt.Component;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.SwingWorker;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
/**
* <p>
* Title: LoonFramework
* </p>
* <p>
* Description:
* </p>
* <p>
* Copyright: Copyright (c) 2007
* </p>
* <p>
* Company: LoonFramework
* </p>
*
* @author chenpeng
* @email:ceponline@yahoo.com.cn
* @version 0.1
*/
public class MyTableModel extends DefaultTableModel {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final ColumnContext[] columnArray = {
new ColumnContext("ID", Integer.class, false),
new ColumnContext("名称", String.class, false),
new ColumnContext("进度", Integer.class, false) };
private final Map<Integer, SwingWorker> swmap = new HashMap<Integer, SwingWorker>();
private int number = 0;
public void addTest(Test t, SwingWorker worker) {
Object[] obj = { new Integer(number), t.getName(), t.getProgress() };
super.addRow(obj);
swmap.put(number, worker);
number++;
}
public synchronized SwingWorker getSwingWorker(int identifier) {
Integer key = (Integer) getValueAt(identifier, 0);
return swmap.get(key);
}
public Test getTest(int identifier) {
return new Test((String) getValueAt(identifier, 1),
(Integer) getValueAt(identifier, 2));
}
public boolean isCellEditable(int row, int col) {
return columnArray[col].isEditable;
}
public Class<?> getColumnClass(int modelIndex) {
return columnArray[modelIndex].columnClass;
}
public int getColumnCount() {
return columnArray.length;
}
public String getColumnName(int modelIndex) {
return columnArray[modelIndex].columnName;
}
private static class ColumnContext {
public final String columnName;
public final Class columnClass;
public final boolean isEditable;
public ColumnContext(String columnName, Class columnClass,
boolean isEditable) {
this.columnName = columnName;
this.columnClass = columnClass;
this.isEditable = isEditable;
}
}
}
class Test {