📄 stablemodel.java
字号:
package cn.com.table;
import javax.swing.table.*;
import java.awt.Color;
import javax.swing.JComponent;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2001</p>
* <p>Company: </p>
* @author
* @version 1.0
*/
public class STableModel extends AbstractTableModel {
private Color[][] cellColor;//单元格颜色数组
private String[] col_name;//表头数组
private String[][] cell_value;//预算数据数组
private String[][] cell_flag;//只读标记数组
public int rowcount;//数据行数,不含表头
public int colcount;//数据列数,不含表头
public static final Color unEditCellColor = new Color(240,240,240); //不可编辑的列或行的颜色
public static final Color isEditCellColor = new Color(255,255,255); //可编辑的列或行的颜色
public static final Color warningIsEditCellColor = new Color(255, 0, 0); //有问题的列或行的颜色
public static final Color warningUnEditCellColor = new Color(240, 180, 160); //有问题的列或行的颜色
public JComponent parent;
public STableModel(CellData rowdata,JComponent parent) {
col_name=rowdata.col_name ;
cell_value=rowdata.cell_value;
cell_flag=rowdata.cell_flag;
colcount=col_name.length;
rowcount=cell_value.length;
this.parent = parent;
setColor();
}
public int[] findNextCell(int row, int column, boolean isShiftDown) {
int[] rowColumn = new int[2];
int c = 0;
int r = 0;
//shift键按下
if (isShiftDown) {
if (column == 0) {
c = colcount - 1;
r = row - 1;
if (r < rowcount) r = r + rowcount;
} else {
c = column - 1;
r = row;
}
while(c != column) {
if (this.isCellEditable(r, c)) {
rowColumn[0] = r;
rowColumn[1] = c;
return rowColumn;
}
c = c - 1;
if (c < 0) {
c = c + colcount;
r = row - 1;
if (r < 0) r = r + rowcount;
}
}//end while
//shift键没有按下
} else {
if (column == colcount - 1) {
c = 0;
r = row + 1;
if (r == rowcount) r = 0;
} else {
c = column + 1;
r = row;
}
while(c != column) {
if (this.isCellEditable(r, c)) {
rowColumn[0] = r;
rowColumn[1] = c;
return rowColumn;
}
c = c + 1;
if (c >= colcount) {
c = c - colcount;
r = row + 1;
if (r >= rowcount) r = r - rowcount;
}
}//end while
}
return rowColumn;
}
//对每个单元格颜色进行保存
private void setColor() {
cellColor = new Color[rowcount][colcount];
for(int i=0;i<rowcount;i++) {
renewColor(i);
// for(int j=0;j<colcount;j++) {
// if(cell_flag[i][j].equalsIgnoreCase("1"))
// cellColor[i][j] = unEditCellColor;
// else
// cellColor[i][j] = isEditCellColor;
// }
}
}
public void renewColor(int row) {
for(int j=0;j<colcount;j++) {
if(cell_flag[row][j].equalsIgnoreCase("1"))
cellColor[row][j] = unEditCellColor;
else
cellColor[row][j] = isEditCellColor;
fireTableCellUpdated(row, j);
}
}
public void setWarningColor(int row) {
for(int j=0;j<colcount;j++) {
if(cell_flag[row][j].equalsIgnoreCase("1"))
cellColor[row][j] = warningUnEditCellColor;
else
cellColor[row][j] = warningIsEditCellColor;
fireTableCellUpdated(row, j);
}
}
public void setColor(int row, int col, Color color) {
cellColor[row][col] = color;
}
public void setDefaultData() {
}
public int getRowCount() {
return rowcount;
}
public int getColumnCount() {
return colcount;
}
public String getColumnName(int column) {
return col_name[column];
}
//是否可以修改
public boolean isCellEditable(int nRow, int nCol) {
if (cell_flag[nRow][nCol].equals("1"))
return false;
else {
//parent.editrow = nRow;
return true;
}
}
//扩展抽象表模型必须重载的,从表模型中取数据给JTable
public Object getValueAt(int nRow, int nCol) {
java.text.DecimalFormat df =new java.text.DecimalFormat(",##0.00"); //四舍五入
/* 如果不想四舍五入就用下面方法。完全舍去。
double f = 111231.5585;
BigDecimal b = new BigDecimal(f);
double f1 = b.setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
System.out.println(f1);
*/
String rv=cell_value[nRow][nCol];
try {
if(nCol>1&&!rv.equalsIgnoreCase("")){//前两列为项目名称和行次,不考虑,只格式化后面数值格式的数据
Double dv=Double.valueOf(rv);
rv=df.format(dv);
}
}
catch (Exception egetx) {//如果单元格数据不不是数值,不能被格式化,则在捕获异常后直接返回未格式化的数据
}
return rv;
}
public Color getColor(int nRow,int nCol) {
return this.cellColor[nRow][nCol];
}
//扩展抽象表模型必须重载的,把JTable中输入的值给表模型
public void setValueAt(Object value, int nRow, int nCol) {
if (nRow < 0 || nRow>=getRowCount())
return;
String svalue = value.toString();
cell_value[nRow][nCol] = new String(svalue);
}
/** @todo 方法_获得所需数据的字符串,使Applet能提供给页面javascript*/
public String getResult() {
String re = "";
for (int i=0;i<this.getRowCount();i++) {
String tempstr = "";
for(int j=2;j<this.getColumnCount();j++){ //前两列为项目名称和行次,不考虑,只传递后面几列的数值信息
tempstr = (i+1)+"&"+(j-1)+"&" +this.cell_value[i][j]+"&"; //传到后台存储过程的下标需调整为以 (1,1)为起始的形式
re = re + tempstr;
}
}
re=re.substring(0,re.length()-2);//把最后一个分隔符"&"去掉
return re;
}
/** @todo 方法_获得所需数据的字符串,使Applet能提供给页面javascript*/
public String getActualResult() {
String re = "";
for (int i=0;i<this.getRowCount();i++) {
String tempstr = "";
for(int j=2;j<this.getColumnCount();j++){ //前两列为项目名称和行次,不考虑,只传递后面几列的数值信息
if(j%2==1){//列表中预算列在前,不传,实际值在后,传
int m=i+1;
int n=(j-1)/2;
tempstr = m+"&"+n+"&" +this.cell_value[i][j]+"&"; //传到后台存储过程的下标需调整为以 (1,1)为起始的形式
re = re + tempstr;
}
}
}
re=re.substring(0,re.length()-2);//把最后一个分隔符"&"去掉
return re;
}
}
// public static final Color unEditCellColor = new Color(240,240,240); //不可编辑的列或行的颜色
// public static final Color isEditCellColor = new Color(255,255,255);
// Color[][] cellColer; //表格颜色
// Object[][] data; //表数据
// Object[] header;//表头
// public STableModel(Object[][] data, Object[] header) {
// this.data = data;
// this.header = header;
// setColor();
// }
// //是否可以修改
// public boolean isCellEditable(int nRow, int nCol) {
// if (nRow == 2) return false;
// return true;
// }
//
// private void setColor() {
// cellColer = new Color[getRowCount()][getColumnCount()];
// for(int i = 0; i < getRowCount(); i++)
// for(int j = 0; j < getColumnCount(); j++)
// cellColer[i][j] = Color.blue;
// }
// public int getRowCount() {
// return data.length;
// /**@todo Implement this javax.swing.table.TableModel abstract method*/
// //throw new java.lang.UnsupportedOperationException("Method getRowCount() not yet implemented.");
// }
// public int getColumnCount() {
// return header.length;
// /**@todo Implement this javax.swing.table.TableModel abstract method*/
// //throw new java.lang.UnsupportedOperationException("Method getColumnCount() not yet implemented.");
// }
// public Object getValueAt(int rowIndex, int columnIndex) {
// return (data[rowIndex][columnIndex]==null)?"":data[rowIndex][columnIndex].toString();
// /**@todo Implement this javax.swing.table.TableModel abstract method*/
// //throw new java.lang.UnsupportedOperationException("Method getValueAt() not yet implemented.");
// }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -