📄 mytable.java
字号:
package com.reason.util.swing.mytable;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.util.*;
/**
* <p>Title: MyTable</p>
* <p>Description: 实现自己的Table,功能如下:</p>
* 由于JTable设置TableColumnModel后是不能正常运行的,所以不采用MyTableColumnModel类!!
* 继承原有JTable的功能
* 自己监听了自己(TableModelListener,TableColumnModelListener, ListSelectionListener)
* 实现了切换排序功能Sorter(用MyTableSotter进行初始化)
* 实现内容: 初始化,设置列宽,选择,排序,增减行列,双击,右键菜单,键盘Enter,键盘Del,键盘F5刷新
* 默认时都采用MyTableSorter的组件
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: pubinfo</p>
* @author chineseren
* @version 1.0
*/
public class MyTable
extends JTable
implements MouseListener, KeyListener {
//获得MyTableModel的所有列名称
public Vector getColumnIdentifiers(){
return this.getMyTableModel().getColumnIdentifiers();
}
public void setColumnIdentifiers(Object[] columnIdentifiers){
this.getMyTableModel().setColumnIdentifiers(columnIdentifiers);
//this.revalidate();
}
public void setColumnIdentifiers(Vector columnIdentifiers){
this.getMyTableModel().setColumnIdentifiers(columnIdentifiers);
//this.revalidate();
}
//MyTable的工具方法: 获取dataVector中第row条记录的第column列的值
protected static Object getValueAtVectors(Vector dataVector,int row, int column){
return ((Vector)dataVector.get(row)).get(column);
}
//用拷贝的方式将dataVector转化成Tbl显示格式,默认什么也不转换由子类具体实现
protected Object[][] formatVectors(Object[][] dataVector){
/** @todo: 比如这样转化dataVector */
return dataVector;
}
//用拷贝的方式将dataVector转化成Tbl显示格式,默认什么也不转换由子类具体实现
protected Vector formatVectors(Vector dataVector){
/** @todo: 比如这样转化dataVector */
/*int iRows = dataVector.size();
Vector rows = new Vector();
for (int i = 0; i < iRows; i++) {
Vector row = new Vector();
row.add(getValueAtVectors(dataVector,i,0));
row.add(getValueAtVectors(dataVector,i,1));
row.add(getValueAtVectors(dataVector,i,4));
row.add(getValueAtVectors(dataVector,i,2));
row.add(getValueAtVectors(dataVector,i,3));
rows.add(row);
}
return rows;*/
return dataVector;
}
//获得MyTableModel的dataVector
public Vector getDataVector(){
return this.getMyTableModel().getDataVector();
}
public void setDataVector(Vector dataVector,Vector columnIdentifiers){
this.getMyTableModel().setDataVector(this.formatVectors(dataVector),columnIdentifiers);
this.selectFirstRow();
this.setColumnWidth();
//this.revalidate();
}
public void setDataVector(Object[][] dataVector,Object[] columnIdentifiers){
this.getMyTableModel().setDataVector(this.formatVectors(dataVector),columnIdentifiers);
this.selectFirstRow();
this.setColumnWidth();
//this.revalidate();
}
public void setDataVector(Vector dataVector){
this.getMyTableModel().setDataVector(this.formatVectors(dataVector),this.getColumnIdentifiers());
this.selectFirstRow();
this.setColumnWidth();
//this.revalidate();
}
//获得指定行row的数据(Vector)rowValue
public Vector getRowValue(int row){
return (Vector)this.getDataVector().get(row);
}
//如果行数比较少(比如<5行)时有问题,当修改这行时还会修改其它几行!!??而且不能让用户拖动列
public void setRowValue(int row,Object[] rowValue){
if(null == rowValue){
return;
}
for (int i = 0; i < rowValue.length; i++) {
this.setValueAt(rowValue[i],row,i);
}
//选中被修改的行
this.selectRow(row);
}
public void setRowValue(int row,Vector rowValue){
this.setRowValue(row,rowValue.toArray());
}
//选中第row行,出错则不动作
public void selectRow(int row){
int count = this.getRowCount();
if(0 >= count || 0 > row || row >= count){
return;
}
this.setRowSelectionInterval(row,row);
}
//选中第一行,出错则不动作
public void selectFirstRow(){
this.selectRow(0);
}
//在第row行增加一行rowValue
public void addRow(int row,Vector rowValue) {
int count = this.getRowCount();
if(row > count){
row = count;
}
this.getMyTableModel().insertRow(row,rowValue);
//选中添加的行
this.selectRow(row);
//触发自己的事件,自己监听
this.onRowAdded(row);
}
public void addRow(int row,Object[] rowValue) {
int count = this.getRowCount();
if(row > count){
row = count;
}
this.getMyTableModel().insertRow(row,rowValue);
//选中添加的行
this.selectRow(row);
//触发自己的事件,自己监听
this.onRowAdded(row);
}
public void addRow(int row,java.util.List rowValue) {
this.addRow(row,rowValue.toArray());
}
public void addRow(Vector rowValue) {
this.addRow(this.getRowCount(),rowValue);
}
public void addRow(Object[] rowValue) {
this.addRow(this.getRowCount(),rowValue);
}
public void addRow(java.util.List rowValue) {
this.addRow(this.getRowCount(),rowValue);
}
//从第row行开始批量添加rowValues行
public void addRows(int row,Vector rowValues) {
int count = this.getRowCount();
if(row > count){
row = count;
}
count = rowValues.size();
for (int i = 0; i < count; i++) {
//rowValues中存放的也是Vector
Vector rowValue = (Vector)rowValues.get(i);
this.addRow(row+i,rowValue);
}
//选中添加的行
this.selectRow(row);
//触发自己的事件,自己监听
this.onRowAdded(row);
}
public void addRows(int row,Object[] rowValues) {
int count = this.getRowCount();
if(row > count){
row = count;
}
count = rowValues.length;
for (int i = 0; i < count; i++) {
//rowValues中存放的也是Object[]
Object[] rowValue = (Object[])rowValues[i];
this.addRow(row+i,rowValue);
}
//选中添加的行
this.selectRow(row);
//触发自己的事件,自己监听
this.onRowAdded(row);
}
public void addRows(int row,java.util.List rowValues) {
//rowValues中存放的也是Object[]
this.addRows(row,rowValues.toArray());
}
public void addRows(Vector rowValues) {
this.addRows(this.getRowCount(),rowValues);
}
public void addRows(Object[] rowValues) {
this.addRows(this.getRowCount(),rowValues);
}
public void addRows(java.util.List rowValues) {
this.addRows(this.getRowCount(),rowValues);
}
/**
* 删除行
* @param row 行号,从0开始
*/
public void removeRow(int row) {
Vector rowValue = this.getRowValue(row);
this.getMyTableModel().removeRow(row);
int count = this.getRowCount();
//选中下一行
if(0 < count) { //还有行
if(row == count){//删除的是最后一行
this.selectRow(row-1);
}else{
this.selectRow(row);
}
}
//触发自己的事件,自己监听
this.onRowRemoved(row,rowValue);
}
//子类继承,当添加完row动作之后
public void onRowAdded(int row){
/**@todo: is onRowAdded && do something */
}
//子类继承,当删除完row动作之后
public void onRowRemoved(int row,Vector rowValue){
/**@todo: is onRowRemoved && do something */
}
/**
* 删除列,出错则不执行动作
* @param column 列号,从0开始
*/
public void removeColumn(int column) {
int count = this.getColumnCount();
if(0 < count){//还有列
TableColumn columnObj = this.getColumnModel().getColumn(column);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -