mytablemodel.java

来自「java绘图 java awt 经典绘图的例子,对于初学awt模块的人非常有帮助」· Java 代码 · 共 81 行

JAVA
81
字号
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package swing;import java.awt.Color;import javax.swing.table.AbstractTableModel;/** * * @author zhaolin */public class MyTableModel extends AbstractTableModel {    @Override    public String getColumnName(int column) {        return columnNames[column];    }    @Override    public Class<?> getColumnClass(int columnIndex) {        return this.getValueAt(0, columnIndex).getClass();    }    @Override    public boolean isCellEditable(int rowIndex, int columnIndex) {        if(columnIndex==1||columnIndex==4){            return true;        }        return false;    }            private Object[][] data = {        {"Mary", Color.RED,    "Snowboarding", new Integer(5), new Boolean(false)},        {"Alison", Color.BLUE,    "Rowing", new Integer(3), new Boolean(true)},        {"Kathy", Color.BLACK,    "Chasing toddlers", new Integer(2), new Boolean(false)},        {"Mark", Color.CYAN,    "Speed reading", new Integer(20), new Boolean(true)},        {"Angela", Color.YELLOW,    "Teaching high school", new Integer(4), new Boolean(false)}    };    private String[] columnNames = {"First Name",        "Last Name",        "Sport",        "# of Years",        "Vegetarian"    };    public int getRowCount() {        return data.length;    }    public int getColumnCount() {        return columnNames.length;    }    public Object getValueAt(int rowIndex, int columnIndex) {        return data[rowIndex][columnIndex];    }    @Override    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {        data[rowIndex][columnIndex] = aValue;        this.fireTableCellUpdated(rowIndex, columnIndex);    }            }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?