📄 rwtablemodel.java
字号:
package com.zcsoft.swing;
import javax.swing.table.DefaultTableModel;
import javax.swing.event.TableModelEvent;
import java.util.Vector;
/**
* 只读或可修改的表格数据模型。默认为只读模式。
*
*/
public class RWTableModel extends DefaultTableModel
{
/** 是否可以编辑标记 */
private boolean cellEditable = false;
/**
* Constructs a default <code>DefaultTableModel</code>
* which is a table of zero columns and zero rows.
*/
public RWTableModel(){}
/**
* Constructs a <code>DefaultTableModel</code> with as many
* columns as there are elements in <code>columnNames</code>
* and <code>numRows</code> of <code>null</code>
* object values. Each column's name will be taken from
* the <code>columnNames</code> array.
*
* @param columnNames <code>array</code> containing the names
* of the new columns. If this is
* <code>null</code> then the model has no columns
* @param numRows the number of rows the table holds
* @see #setDataVector
* @see #setValueAt
*/
public RWTableModel(Object[] columnNames, int numRows)
{
super(columnNames, numRows);
}
/**
* Returns whether the cells is editble.
*
* @param row the row whose value is to be queried
* @param column the column whose value is to be queried
* @return true
* @see #setCellEditable(boolean)
*/
public boolean isCellEditable(int row, int column)
{
return cellEditable;
}
/**
* Sets whether all cells is editable.
* @param editable whether the cells is editable.
*/
public void setCellEditable(boolean editable)
{
this.cellEditable=editable;
}
/**
* Replaces the current <code>dataVector</code> instance variable with the
* new Vector of rows, <code>newData</code>.
* Each row in <code>newData</code>
* is adjusted to match the number of columns in <code>columnNames</code>
* either by truncating the <code>Vector</code> if it is too long,
* or adding <code>null</code> values if it is too short.
* <p>
*
* @param newData the new data vector
*/
public void setDataVector(Vector newData)
{
//不是dataVector.setSize(0);为的是删除时会激发一些事件
//不能这样处理,因为这样做破坏了原来的数据。而设置该属性
//不应该影响外部资源
//this.setNumRows(0);
//直接调用事件通知就可以了
if(this.getRowCount() > 0)
{
fireTableRowsDeleted(0, this.getRowCount() - 1);
}
if (newData == null)
{
//debug 03-08-17 蒋智湘
//return;
dataVector = new Vector(0);
}
else
{
dataVector = newData;
}
newRowsAdded(new TableModelEvent(this, 0, getRowCount()-1,
TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
}
/**
* Replaces the current <code>dataVector</code> instance variable with the
* new Vector of rows, <code>newData</code>.
* <code>columnNames</code> are the names
* of the new columns. The first name in <code>columnNames</code> is
* mapped to column 0 in <code>newData</code>. Each row in
* <code>newData</code>
* is adjusted to match the number of columns in <code>columnNames</code>
* either by truncating the <code>Vector</code> if it is too long,
* or adding <code>null</code> values if it is too short.
* <p>
*
* @param newData the new data vector
* @param columnNames the names of the columns
* @see #getDataVector
*/
public void setDataVector(Vector newData, Vector columnNames)
{
if(newData==null)
newData = new Vector(0);
super.setDataVector(newData,columnNames);
}
/**
* 添加多行记录。
* @param rows 每个成员为Vector实例的Vector实例或null。
* 确保每个成员的size同数据模型的列数相等。
*
*/
public void addRows(Vector rows)
{
int appendCnt = rows != null?rows.size():0;
if(appendCnt < 1)
return;
int old = getRowCount();
//--姚立成 2003-10-30日改,为了统一增加接口
for (int i = 0; i < rows.size(); i++)
{
addRow((Vector)rows.get(i));
}
//dataVector.addAll(rows);
// Generate notification
newRowsAdded(new TableModelEvent(this, old, getRowCount() - 1,
TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
}
/**
* 用指定的数据行向量替换原有指定行上的全部内容
* @param row 待更新行的索引
* @param newRow 替换后的数据行向量。如果为null,则视同空行即new Vector()
*/
public void replaceRow(int row, Vector newRow)
{
if(newRow == null)
{
newRow = new Vector();
}
newRow.setSize(this.getColumnCount());
dataVector.set(row, newRow);
fireTableChanged(new TableModelEvent(this, row, row, TableModelEvent.ALL_COLUMNS));
}
/**
* 删除多行记录。
* 真正删除前,方法完成对rowIndexes进行按升序排序操作,
* 确保从低索引往高索引方向依个删除。
* @param rowIndexes 要删除的行的索引。各索引的值为删除前的索引值。
* @exception IndexOutOfBoundsException 某个索引值rowIndexes[i]不存在
*
*/
public void removeRows(int rowIndexes[])
{
if (rowIndexes == null || rowIndexes.length < 1 )
{
return;
}
java.util.Arrays.sort(rowIndexes);//排序很快
synchronized(dataVector)
{
int len = rowIndexes.length;
int cnt = dataVector.size();
Vector nw = new Vector(cnt - len);//在原有数据行上删除待删除行后的数据
if (len == cnt)
{
this.setDataVector(nw);
}
else//如果数据很多,则很慢
{
int start = -1,
end = 0;
for (int i = 0; i < len; i++)
{
end = rowIndexes[i];
if (end - start > 1)//不是相邻的两行,则保留之间的行
{
nw.addAll(dataVector.subList(start+1, end));
}
start = end;
}
if (++end < cnt)//要删除的最末行后还有不删除的行
{
nw.addAll(dataVector.subList(end, cnt));
}
this.setDataVector(nw);
}
}
}
/**
* 获取模型的中一整行表格单元数据构成的向量
* @param index
* @return
*/
public Vector getRow(int index)
{
return (Vector)dataVector.get(index);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -