关于 JTable 刷新绘制(repaint) java swing
说明:
本文典型应用于,当数据发生改变时,重新加载JTable数据,并且刷新,
令使用者可以立刻看到改变的结果。
使用JTable展示数据时,本部分对数据的增、删、改、查所引起的数据列
表的改变,可以进行实时刷新展示。而不论改变动作来自于本窗体还是外
来窗体。
1. 关于 JTable 的数据装载 以及 关于响应JTable的某些事件
说明:此部分包含构建java JTable表格;
响应表格变化引起的事件;
响应鼠标点击表格引起的事件;
1.1 构建Jtable 数据模型函数
1.1.1 构建Jtable title
此函数用于购建表格台头部分
public String[] jtableTitle() {
int i;
String[] jtableTitle = new String[i];
/** 'i' means you will create table's column amount */
stringUser[0] = "TableColumn1";
stringUser[1] = "TableColumn2";
.......
stringUser[i-1] = "TableColumni-1";
return jtableTitle;
}
1.1.2 构建Jtable data structure
此函数用于购建表格数据结构部分,即往表格中填入数据:
public Object[][] dataStruct() throws SQLException {
/** throws SQLException means data from some db's table */
Object[][] dataStruct=null;
db.connect(); /** some link database code in here */
ResultSet rs = db.exeQuery("select * from tablename");
ResultSet rscount = db.exeQuery("select count(*) from tablename");
if (rs==null){emptyType=0;}else{emptyType=1;}
/** 'emptyType' tell programe table's data whether empty*/
switch (emptyType){
case 0:
dataUser = new Object[1][i];
dataUser[0][0]="null";
dataUser[0][1]="null";
dataUser[0][2]="null";
.....................
dataUser[0][i-1]="null";
rsemptyType=true;
/** means db's table is empty. nothing data will fill in JTable */
break;
case 1:
rscount.next();
int j = rscount.getInt(1);
/** means get table's data count amount*/
dataUser = new Object[j][i];
for (int x = 0; x < j; x++) {
rs.next();
dataUser[x][0] = rs.getString(dbTableColumnName1);
dataUser[x][1] = rs.getString(dbTableColumnName2);
dataUser[x][2] = rs.getString(dbTableColumnName3);
...........................
dataUser[x][i-1] = rs.getString(dbTableColumnNamei);
rsemptyType=false;
/** means db's table have some data and code fill these data in JTable */
db.disconnect();
return dataStruct;
}
1.1.3 将构建好的数据加载入Jtable
final JTable jTable1 = new JTable(dataStruct(),jtableTitle())
1.2 JTable valueChanged() 监听事件
此函数用于监听当JTable发生变化时的事件
典型的应用,适用于将选中的Jtable的行数据,读入窗体中的text,combo等等控件。
方便进行修改(modify)或者删除(delete)操作。
public void valueChanged(javax.swing.event.ListSelectionEvent e) {
super.valueChanged(e);
/** super means Jtable */
if (!this.isVisible()) { return; } /** if Jtable visible is false. stop execute code*/
if (!rsemptyType()) { return; }
/** get from 1.1.2 function 'dataStruct()' to judge data dataStruct whether null or have some data */
ListSelectionModel sm = (ListSelectionModel)e.getSource();
int row = sm.getLeadSelectionIndex(); /** means get selected row number */
int colCount = this.getColumnCount(); /** means get Jtable's Column amount */
String[] sa = new String[colCount];
for (int col=0; col
sa[col] = (String)this.getValueAt(row, col);
}
jTextField1.setText(sa[0]);
jTextField2.setText(sa[1]);
jTextField3.setText(sa[2]);
...........................
jTextField4.setText(sa[colCount-1]);
/** above code means fill the selected row's data in text control by every column*/
}
1.3 JTable isCellEditable() 函数
此函数为重新构建的Jtable's isCellEditable属性。
此函数典型应用于:当你需要使客户不可对表格内容进行编辑时,可以使用此函数。
public boolean isCellEditable(int row,int col){return false;}
1.4 JTable MouseListener()监听事件
此函数用于监听当鼠标点击JTable时发生的事件 .
jTable1.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseClicked(java.awt.event.MouseEvent evt)
{
if (!rsemptyType()) { return; }
/** get from 1.1.2 function 'dataStruct()' to judge data dataStruct whether null or have some data */
Point mousepoint;
mousepoint = evt.getPoint();
int row=jTable1.rowAtPoint(mousepoint)+1;
int col=jTable1.columnAtPoint(mousepoint)+1;
/** get row and column which mouse click into */
............ //some code to run which you need
}
2. JTable 重新绘制(repaint)
此函数用于在每一次对查询出的数据结果集进行改变时,将Jtable重新绘制并显示。
当然,亦可用作第一次加载,是一个绘制Jtable的完整函数。
public static void repainTable() throws SQLException{
/** set this fuction is a static type, then other form can use it. */
final JTable jTable1 = new JTable(jtableTitle() ,dataStruct()){
/** fill data struct in this JTable. */
public void valueChanged(javax.swing.event.ListSelectionEvent e) {....... /** detail in 1.2 */}
public boolean isCellEditable(int row,int col){return false;}
};
jTable1.addMouseListener(new java.awt.event.MouseAdapter() { ......../** detail in 1.4 */}
/** follow sentences set JTable's row Height*/
jTable1.setRowHeight(25);
/** follow code control JTable's Column Width*/
jTable1.getColumn("ColumnName").setMaxWidth(80);
jTable1.getColumn("ColumnName").setMinWidth(80);
/** notification:when you will set Column's Width please use setMaxWidth and set setMinWidth
Jtable.setWidth can't change Column's Width */
/** follow sentences set JTable title height*/
jTable1.getTableHeader().setPreferredSize(new Dimension(1,25));
/** follow sentences set JTable's Background color*/
jTable1.setBackground(new Color(223,238,249));
/** follow sentences set JTable's word font color*/
jTable1.setForeground(new Color(0,0,0));
/** follow sentences set JTable's SideLine/GridLine font color*/
jTable1.setGridColor(new Color(100,100,100));
/** follow sentences set JTable's Title Background and font color*/
/** Get JTable's Title*/
JTableHeader tableH = jTable1.getTableHeader();
tableH.setBackground(new Color(26,90,142));
tableH.setForeground(new Color(255,255,255));
jTable1.setIntercellSpacing(new Dimension(5,5));
/** follow sentences paint JTable on your from*/
jScrollPane1.setViewportView(jTable1);
}
由上可见,此函数亦设置了Jtable 行高、列宽、Jtable台头高度、台头字体、台头背景颜色。
Jtable本身背景颜色、未选中字体颜色、行、列分隔线颜色。以供参考。
备注:
本文系本人java开发随笔记录,因尚在学习阶段,恐有遗忘,特写此提醒,请勿转载,以免贻笑方家。
本文书写格式只是个人喜好,表达不清之处,错漏之处,欢迎指出,供个人学习、改正。