📄 yarntablemodel.java
字号:
package yarn;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
import javax.swing.table.*;
import javax.swing.table.AbstractTableModel;
import java.util.*;
public class YarnTableModel extends AbstractTableModel{
Vector rowData = new Vector();
String[] columnName;
//可编辑判断条件
boolean isEdit = false;
int whatTable = 0;
public YarnTableModel(Vector rowData,String[] columnName) {
this.rowData = rowData;
this.columnName = columnName;
}
//特定表格的表格模式构造方法
public YarnTableModel(Vector rowData,String[] columnName,int whatTable) {
this.whatTable = whatTable;
this.rowData = rowData;
this.columnName = columnName;
}
//返回默认编辑器和渲染器类
public Class getColumnClass(int c)
{
if (c == 5&&((Object[])rowData.elementAt(0))[5]!=null)
return getValueAt(0,c).getClass();
else return super.getClass();
}
//实现抽象类AbstractTableModel的getColumnCount()方法
public int getColumnCount()
{
return columnName.length;
}
//实现抽象类AbstractTableModel的public String getColumnName(int columnIndex)方法
public String getColumnName(int columnIndex)
{
return columnName[columnIndex];
}
//实现抽象类AbstractTableModel的public int getRowCount()方法
public int getRowCount()
{
return rowData.size();
}
//实现抽象类AbstractTableModel的public int getRowCount()方法
public Object getValueAt(int rowIndex,int columnIndex)
{
return (Object)((Object[])rowData.elementAt(rowIndex))[columnIndex];
}
//重载isCellEditable方法使表格布可编辑
public boolean isCellEditable(int rowIndex, int columnIndex)
{
if (whatTable == 2)
{
if(columnIndex == 1||columnIndex ==4||columnIndex ==5||columnIndex ==6||columnIndex ==7)
return true;
else
return false;
}
return true;
}
//重载setValueAt方法使表格可更新为新数据
public void setValueAt(Object value, int row, int col)
{
//临时Object数组对象
Object[] temp;
temp = (Object[])rowData.elementAt(row);
temp[col] = value;
rowData.set(row,temp);
fireTableCellUpdated(row, col);
}
//设置表格可否编辑,默认不能编辑
public void setEditEnable(boolean isEdit)
{
this.isEdit = isEdit;
}
//返回表格的数据
public Vector getVector()
{
return rowData;
}
//设置表格数据
public void setVector(Vector rowData)
{
this.rowData = rowData;
this.fireTableDataChanged();
}
//增加明细表的行数
public void addTableItem()
{
//当表格不为空时添加物品情况
if(rowData.size()>0)
{
//只要在表格的最后一行填写完才能增加明细行
if (((Object[]) rowData.elementAt(rowData.size()-1))[2] !=null) {
Object[] tempS = new Object[8];
tempS[0] = rowData.size() + 1;
tempS[5] = new Boolean(false);
tempS[6] = 0.0;
tempS[7] = 0.0;
rowData.addElement(tempS);
this.setVector(rowData);
}
}
//如果表格为空把表格增加一行
else
{
Object[] tempS = new Object[8];
tempS[0] = rowData.size() + 1;
tempS[5] = new Boolean(false);
rowData.addElement(tempS);
this.setVector(rowData);
}
}
//填写最后一个没有填写的行
public void setLastNonFillRow(Object[] nfRow)
{
if(rowData.size()>0)
{
//如果表格中最后一项的纱线基本数据已经填写的话把表格增加两行其中一行写入选择的纱线品种
if (((Object[]) rowData.elementAt(rowData.size()-1))[2] ==null)
{
nfRow[0] = rowData.size();
rowData.setElementAt(nfRow,rowData.size()-1);
Object[] tempS = new Object[8];
tempS[0] = rowData.size() + 1;
rowData.addElement(tempS);
this.setVector(rowData);
}
else
{
nfRow[0] = rowData.size() + 1;
rowData.addElement(nfRow);
Object[] tempS = new Object[8];
tempS[0] = rowData.size() + 1;
rowData.addElement(tempS);
this.setVector(rowData);
}
}
//如果表格为空把表格增加一行
else
{
nfRow[0] = rowData.size() + 1;
rowData.addElement(nfRow);
Object[] tempS = new Object[8];
tempS[0] = rowData.size() + 1;
rowData.addElement(tempS);
this.setVector(rowData);
}
}
//删除制定的行
public void deleteRows(int[] sRows)
{
int i = 0,j = 0;
//判断是否成功执行删除操作
boolean s =(boolean) false;
if( s == false)
{
while (i < sRows.length)
{
rowData.removeElementAt(sRows[i]-i);
i++;
}
s = true;
}
if(s == true)
{
while (j < rowData.size())
{
setValueAt(j+1,j,0);
j++;
}
s = false;
}
this.fireTableDataChanged();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -