重载AbstractTableModel public class ResultTableModel extends AbstractTableModel { private String[] columnNames = { .... }; Vector<对应数据库表类> data = new Vector<...>(); public int getColumnCount() { return columnNames.length; } public int getRowCount() { return data.size(); } public String getColumnName(int col) { return columnNames[col]; } public Object getValueAt(int row, int col) { Object value = null; switch (col) { case 0: value = data.get(row).get字段1(); break; case 1: value = data.get(row).get字段2(); break;
} return value; } /* * JTable uses this method to determine the default renderer/ editor for * each cell. If we didn't implement this method, then the last column would * contain text ("true"/"false"), rather than a check box. */ @SuppressWarnings("unchecked") public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } /* * Don't need to implement this method unless your table's editable. */ public boolean isCellEditable(int row, int col) { // Note that the data/cell address is constant, // no matter where the cell appears onscreen. if (col < 2) { return false; } else { return true; } } /* * Don't need to implement this method unless your table's data can change. */ public void setValueAt(Object value, int row, int col) { switch (col) { case 0: data.get(row).set字段1((String) value); break; case 1: data.get(row).set字段2((String) value); break; //.... } fireTableCellUpdated(row, col); } public void addRow(类 r) { data.add(r); this.fireTableDataChanged(); } public void clear() { if (!data.isEmpty()) data.clear(); this.fireTableDataChanged(); }}
一个隐藏,一个显示全部
其实只要控制TableModel就什么问题都可以解决。
tableModel中搞两个Data
一个隐藏,一个显示全部
___________不好意思 再问下 怎么搞两个data在一个model里啊?
public class ResultTableModel extends AbstractTableModel {
private String[] columnNames = { .... }; Vector<对应数据库表类> data = new Vector<...>(); public int getColumnCount() {
return columnNames.length;
} public int getRowCount() {
return data.size();
} public String getColumnName(int col) {
return columnNames[col];
} public Object getValueAt(int row, int col) {
Object value = null; switch (col) {
case 0:
value = data.get(row).get字段1();
break;
case 1:
value = data.get(row).get字段2();
break;
} return value;
} /*
* JTable uses this method to determine the default renderer/ editor for
* each cell. If we didn't implement this method, then the last column would
* contain text ("true"/"false"), rather than a check box.
*/
@SuppressWarnings("unchecked")
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
} /*
* Don't need to implement this method unless your table's editable.
*/
public boolean isCellEditable(int row, int col) {
// Note that the data/cell address is constant,
// no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
} /*
* Don't need to implement this method unless your table's data can change.
*/
public void setValueAt(Object value, int row, int col) { switch (col) {
case 0:
data.get(row).set字段1((String) value);
break;
case 1:
data.get(row).set字段2((String) value);
break;
//....
} fireTableCellUpdated(row, col); } public void addRow(类 r) {
data.add(r);
this.fireTableDataChanged();
} public void clear() {
if (!data.isEmpty())
data.clear();
this.fireTableDataChanged();
}}