代码改变世界

继承AbstractTableModel

2012-11-28 00:36  youxin  阅读(408)  评论(0编辑  收藏  举报
public abstract class AbstractTableModelextends Objectimplements TableModel, Serializable

This abstract class provides default implementations for most of the methods in the TableModel interface. It takes care of the management of listeners and provides some conveniences for generating TableModelEvents and dispatching them to the listeners. To create a concrete TableModel as a subclass of AbstractTableModel you need only provide implementations for the following three methods:

  public int getRowCount();
  public int getColumnCount();
  public Object getValueAt(int row, int column);

 

class TableValues extends AbstractTableModel{
    //public final static int FIRST_NAME = 0;////public final static int LAST_NAME = 1;////public final static int DATE_OF_BIRTH = 2;//生日
    //public final static int ACCOUNT_BALANCE = 3;//balance:平衡
    //public final static int GENDER = 4;//性别
    public final static boolean GENDER_MALE = true;//
    public final static boolean GENDER_FEMALE = false;//
    public Object[][] values = {
              { "康", "杜", new GregorianCalendar(1962, Calendar.FEBRUARY, 20).getTime(),
                  new Float(15.67), new Boolean(GENDER_MALE) },
              { "白", "李", new GregorianCalendar(1987, Calendar.JANUARY, 6).getTime(),
                  new Float(2.78), new Boolean(GENDER_MALE) },
              { "中山", "孙", new GregorianCalendar(1989, Calendar.AUGUST, 31).getTime(),
                  new Float(3.89), new Boolean(GENDER_FEMALE) },
              { "学良", "张", new GregorianCalendar(1945, Calendar.JANUARY, 16).getTime(),
                  new Float(-4.70), new Boolean(GENDER_FEMALE) },
              { "谨", "秋", new GregorianCalendar(1907, Calendar.AUGUST, 2).getTime(),
                  new Float(5.00), new Boolean(GENDER_FEMALE) }};

    public int getColumnCount() {//拿到列
        return values.length;
    }
    public int getRowCount() {//拿到行数
        return values[0].length;//values[0]的列数是:5
    }

    public Object getValueAt(int row, int col) {//拿到单元格的值
        return values[row][col];
    }
    
}

public class ExtendingAbstractTableModel extends JFrame{
    protected JTable table;

    public static void main(String[] args) {
        ExtendingAbstractTableModel eat = new ExtendingAbstractTableModel();
        eat.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        eat.setSize(400,200);
        eat.setVisible(true);
    }
    public ExtendingAbstractTableModel(){
        Container pane = getContentPane();
        pane.setLayout(new BorderLayout());
        TableValues tv = new TableValues();
        table = new JTable(tv);
        pane.add(table,BorderLayout.CENTER);
    }
}

http://blog.csdn.net/youyigong/article/details/6890100