21 tháng 11 2006

TableModel Standard

class MyModel extends AbstractTableModel{

private String[] _columns = new String[]{"Id", "Name"};
private Class[] _classnames = new Class[]{Integer.class,String.class};
private Vector _data ;

public MyModel( Vector data ){
_data = data;
}

public int getRowCount() {
return _data.size();
}

public int getColumnCount() {
return _columns.length;
}

public Object getValueAt(int rowIndex, int columnIndex) {
MyObject obj = _data.get( rowIndex );
switch ( columnIndex ){
case 0:
return obj.getId();
case 1:
return obj.getName();
}
return null;
}

public String getColumnName(int col)
{
return _columns[col];
}

public Class getColumnClass(int col)
{
return _classnames[ col ];
}

public void addNewItem( MyObject obj ){
_data.add( obj );
this.fireTableRowsInserted( this.getRowCount(), this.getRowCount() );
this.fireTableDataChanged();
}
}

class MyObject{
private int _id;
private String _name;

public void setId( int id ){
_id = id;
}

public void setName ( String name ){
_name = name;
}

public int getId(){
return _id;
}

public String getName(){
return _name;
}
}

Không có nhận xét nào: