在JTable中加载进度条及进行操作

我们在多文件下载或多事务处理时,经常会出现使用线程以提高效率的情况,而这时在GUI程序中如何表示进度,就成了一个不大不小的问题。
现在比较被大众接受的方式,大体就是如迅雷等下载工具中一样,用表格中加载进度条以进行显示。
而对于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>
*
*
@authorchenpeng
*@email:[email protected]
*
@version0.1
*/

public class MyTableModel extends DefaultTableModel ... {
/***//**
*
*/

privatestaticfinallongserialVersionUID=1L;

privatestaticfinalColumnContext[]columnArray=...{
newColumnContext("ID",Integer.class,false),
newColumnContext("名称",String.class,false),
newColumnContext("进度",Integer.class,false)}
;

privatefinalMap<Integer,SwingWorker>swmap=newHashMap<Integer,SwingWorker>();

privateintnumber=0;

publicvoidaddTest(Testt,SwingWorkerworker)...{
Object[]obj
=...{newInteger(number),t.getName(),t.getProgress()};
super.addRow(obj);
swmap.put(number,worker);
number
++;
}


publicsynchronizedSwingWorkergetSwingWorker(intidentifier)...{
Integerkey
=(Integer)getValueAt(identifier,0);
returnswmap.get(key);
}


publicTestgetTest(intidentifier)...{
returnnewTest((String)getValueAt(identifier,1),
(Integer)getValueAt(identifier,
2));
}


publicbooleanisCellEditable(introw,intcol)...{
returncolumnArray[col].isEditable;
}



publicClass<?>getColumnClass(intmodelIndex)...{
returncolumnArray[modelIndex].columnClass;
}


publicintgetColumnCount()...{
returncolumnArray.length;
}


publicStringgetColumnName(intmodelIndex)...{
returncolumnArray[modelIndex].columnName;
}


privatestaticclassColumnContext...{
publicfinalStringcolumnName;

publicfinalClasscolumnClass;

publicfinalbooleanisEditable;

publicColumnContext(StringcolumnName,ClasscolumnClass,
booleanisEditable)...{
this.columnName=columnName;
this.columnClass=columnClass;
this.isEditable=isEditable;
}

}

}


class Test ... {
privateStringname;

privateIntegerprogress;

publicTest(Stringname,Integerprogress)...{
this.name=name;
this.progress=progress;
}


publicvoidsetName(Stringstr)...{
name
=str;
}


publicvoidsetProgress(Integerstr)...{
progress
=str;
}


publicStringgetName()...{
returnname;
}


publicIntegergetProgress()...{
returnprogress;
}

}


class ProgressRenderer extends DefaultTableCellRenderer ... {
/***//**
*
*/

privatestaticfinallongserialVersionUID=1L;
privatefinalJProgressBarb=newJProgressBar(0,100);

publicProgressRenderer()...{
super();
setOpaque(
true);
b.setBorder(BorderFactory.createEmptyBorder(
1,1,1,1));
}


publicComponentgetTableCellRendererComponent(JTabletable,Objectvalue,
booleanisSelected,booleanhasFocus,introw,intcolumn)...{
Integeri
=(Integer)value;
Stringtext
="完成";
if(i<0)...{
//删除
text="取消完毕";
}
elseif(i<100)...{
b.setValue(i);
returnb;
}

super.getTableCellRendererComponent(table,text,isSelected,hasFocus,
row,column);
returnthis;
}

}


MyPanel.java

package org.loon.test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.HashSet;
import java.util.Random;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.SwingWorker;
import javax.swing.WindowConstants;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableRowSorter;

// importorg.loon.framework.dll.NativeLoader;

/***/ /**
*<p>
*Title:LoonFramework
*</p>
*<p>
*Description:
*</p>
*<p>
*Copyright:Copyright(c)2007
*</p>
*<p>
*Company:LoonFramework
*</p>
*
*
@authorchenpeng
*@email:[email protected]
*
@version0.1
*/

public class MyPanel extends JPanel ... {
/***//**
*
*/

privatestaticfinallongserialVersionUID=1L;

privatestaticfinalColorevenColor=newColor(250,250,250);

privatefinalMyTableModelmodel=newMyTableModel();

privatefinalTableRowSorter<MyTableModel>sorter=newTableRowSorter<MyTableModel>(
model);

privatefinalJTabletable;

publicMyPanel()...{
super(newBorderLayout());
table
=newJTable(model)...{
/***//**
*
*/

privatestaticfinallongserialVersionUID=1L;

publicComponentprepareRenderer(
TableCellRenderertableCellRenderer,
introw,intcolumn)...{
Componentcomponent
=super.prepareRenderer(tableCellRenderer,row,
column);
//背景色及字体设置
if(isRowSelected(row))...{
component.setForeground(getSelectionForeground());
component.setBackground(getSelectionBackground());
}
else...{
component.setForeground(getForeground());
component.setBackground((row
%2==0)?evenColor:table
.getBackground());
align=
分享到:
评论

你可能感兴趣的:(java,jdk,多线程,swing,Yahoo)