📄 mytable.java
字号:
super.removeColumn(columnObj);
}
}
//对MyTable的风格进行初始化,由构造函数调用
protected void initTable(){
this.setColumnWidth();
this.setFont(new Font("Serif",0,14));
this.setRowHeight(20);
this.selectFirstRow();//实现 选中第一行
this.getTableHeader().setReorderingAllowed(false);//实现 默认不能让用户拖动列
try {
this.addMouseListenerToHeader(); //实现 排序
} catch(Exception ex) {
//说明TableModel不是MyTableSorter
}
this.addMouseListener(this);//实现 双击,右键菜单
this.addKeyListener(this);//实现 键盘Enter,键盘Del,键盘F5刷新
}
public MyTable() {
super();
//默认是实现MyTableSorter的
this.setModel(new MyTableSorter());
//this.setColumnModel(new MyTableColumnModel());
this.setSelectionModel(new MyTableSelectionModel());
this.initTable();
}
public MyTable(Dimension size) {
this();
this.setPreferredScrollableViewportSize(size);
}
public MyTable(MyTableModel dm) {
super(dm);
//this.setColumnModel(new MyTableColumnModel());
this.setSelectionModel(new MyTableSelectionModel());
this.initTable();
}
public MyTable(MyTableModel dm, MyTableSelectionModel sm) {
super(dm);
this.setSelectionModel(sm);
this.initTable();
}
/*public MyTable(MyTableModel dm, MyTableColumnModel cm) {
super(dm,cm);
this.setSelectionModel(new MyTableSelectionModel());
this.init();
}
public MyTable(MyTableModel dm, MyTableColumnModel cm, MyTableSelectionModel sm) {
super(dm,cm,sm);
this.init();
}*/
public MyTable(int numRows, int numColumns) {
this(new MyTableSorter(numRows,numColumns));
}
public MyTable(Vector rowData, Vector columnNames) {
this(new MyTableSorter(rowData,columnNames));
}
public MyTable(Object[][] rowData, Object[] columnNames) {
this(new MyTableSorter(rowData,columnNames));
}
//默认带了一个JScrollPane
private JScrollPane tablePane = new JScrollPane();
/**
* 获得table的scrollPane
* @param size table的大小
* @return JScrollPane(包含了MyTable)
*/
public JScrollPane getMyTablePane(Dimension size){
this.tablePane.getViewport().add(this);
this.tablePane.setPreferredSize(size);
return this.tablePane;
}
public JScrollPane getMyTablePane(int width, int height){
return this.getMyTablePane(new Dimension(this.getWidth(),this.getHeight()));
}
public JScrollPane getMyTablePane(){
return this.getMyTablePane(this.getWidth(),this.getHeight());
}
/**
* 设定列宽
*/
public void setColumnWidth() {
/**@todo: 根据table的实际情况单个设置(让子类来继承,由默认构造函数调用)*/
}
public void setColumnWidth(int[] widths) {
TableColumn column = null;
int colCount = this.getColumnCount();
if(null == widths || colCount > widths.length){
return;
}
for(int i = 0; i < colCount; i++) {
column = this.getColumnModel().getColumn(i);
column.setPreferredWidth(widths[i]);
}
}
//获得MyTableModel
public MyTableModel getMyTableModel(){
return (MyTableModel)this.getModel();
}
//获得MyTableSorter
public MyTableSorter getMyTableSorter(){
return (MyTableSorter)this.getModel();
}
//获得MyTableColumnModel
public MyTableColumnModel getMyTableColumnModel(){
return (MyTableColumnModel)this.getColumnModel();
}
//获得MyTableSelectionModel
public MyTableSelectionModel getMyTableSelectionModel(){
return (MyTableSelectionModel)this.getSelectionModel();
}
// Add a mouse listener to the Table to trigger a table sort
// when a column heading is clicked in the JTable.
public void removeMouseListenerFromHeader(){
//全部移除
JTableHeader th = this.getTableHeader();
MouseListener[] ls = th.getMouseListeners();
if(null != ls){
for (int i = 0; i < ls.length; i++) {
th.removeMouseListener(ls[i]);
}
}
}
public void addMouseListenerToHeader() {
this.getMyTableSorter().addMouseListenerToHeaderInTable(this);
}
//MyTable中包含了一个JPopupMenu,子类可以替换掉它
private JPopupMenu popupMenu = new JPopupMenu("表格菜单");
public JPopupMenu getPopupMenu(){
return this.popupMenu;
}
public void setPopupMenu(JPopupMenu popupMenu){
this.popupMenu = popupMenu;
}
public void addMenuItemToPopupMenu(JMenuItem menuItem){
this.popupMenu.add(menuItem);
}
//实现MouseListener: 双击,右键菜单
public void mouseClicked(MouseEvent e) {
if(e.getClickCount() == 2 && this.getSelectedRow() != -1) {
this.onDoubleClicked(e);
}else{
this.onClicked(e);
}
}
public void onDoubleClicked(MouseEvent e){//双击之后,显示详细信息Dlg,修改
/**@todo: is double click && do something */
}
public void onClicked(MouseEvent e) { //单击之后,选中行,可能弹出右键菜单
//let's select it!
int rowSeled = this.rowAtPoint(new Point(e.getX(), e.getY()));
if( -1 != rowSeled) {
this.selectRow(rowSeled);
}
if(-1 != this.getSelectedRow() && e.isPopupTrigger()) {
this.popupMenu.show(e.getComponent(),e.getX(),e.getY());
}
}
public void mousePressed(MouseEvent e) {
onClicked(e);
}
public void mouseReleased(MouseEvent e) {
onClicked(e);
}
public void mouseEntered(MouseEvent e) {
/**@todo: is mouseEntered && do something */
}
public void mouseExited(MouseEvent e) {
/**@todo: is mouseExited && do something */
}
//删除行之前是否要询问,默认要问
private boolean isAsk = true;
public boolean getIsAsk(){
return isAsk;
}
public void setIsAsk(boolean isAsk){
this.isAsk = isAsk;
}
//实现KeyListener: 键盘Enter,键盘Del,键盘F5刷新
public void keyTyped(KeyEvent e) {
/**@todo: is keyTyped && do something */
}
public void keyReleased(KeyEvent e) {
/**@todo: is keyReleased && do something */
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch(key) {
case KeyEvent.VK_ENTER:
if(-1 != this.getSelectedRow()) {
onKeyEnter(e);
}
return;
case KeyEvent.VK_DELETE:
int row = this.getSelectedRow();
if( -1 != row && (!this.isAsk ||
JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(this,
"您确实要删除第 " + row + " 条数据吗?", "删除数据",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE))) {
onKeyDelete(e);//doBussiness(),没有考虑到事务操作,比如返回操作是否成功
this.removeRow(row);//删除行
}
return;
case KeyEvent.VK_F5:
this.revalidate();
onKeyF5(e);
return;
default:
return;
}
}
//键盘Enter,键盘Del,键盘F5刷新
public void onKeyEnter(KeyEvent e){//相当于双击鼠标onDoubleClick
onDoubleClicked(null);
}
public void onKeyDelete(KeyEvent e){//相当于删除行之前提示之后按了"是"
/**@todo: is onKeyDelete && do something */
}
public void onKeyF5(KeyEvent e){//相当于刷新MyTable,重新查询数据
this.refresh();
}
//F5刷新MyTable
public void refresh(){
this.setDataVector(this.getDataVector());
this.selectFirstRow();
this.revalidate();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -