事情是这样的:从数据库拿到数据显示在JTable上,但是没有显示所有的列。当点击“详细”按钮时,才会显示这一行的所有烈的数据。我想问可以把不显示的列保存在TableModel中么?这样点击“详细”按钮时就可以不用再操作数据库了~可是我不知道该怎么隐藏列,有什么好方法么?
谢萨

解决方案 »

  1.   

    http://www.finereport.com 电子表格控件
      

  2.   

    tableModel中搞两个Data
    一个隐藏,一个显示全部
    其实只要控制TableModel就什么问题都可以解决。
      

  3.   

    shengli_liao(我是谁?) 
     tableModel中搞两个Data
     一个隐藏,一个显示全部
    ___________不好意思 再问下 怎么搞两个data在一个model里啊?
      

  4.   

    重载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();
    }}