2012-11-08 130 views
4

我創建了一個擴展DefaultTableModel的表模型。通過自定義表格模型將行添加到jtable中

public class TableModel extends DefaultTableModel { 
List<VariableDetails> data; 
public AttachedVariableTableModel(){ 
super();   
this.data=Collections.synchronizedList(new ArrayList<VariableDetails>()); 
} 

//method for adding rows 
public void addRow(VariableDetails varDetails){ 

    this.data.add(varDetails); 
    fireTableRowsInserted(this.data.size()-1,this.data.size()-1); 
    } 

} 

我試圖添加行到已經有數據的表中。

tModel.addRow(new VariableDetails()); 

但行無法添加。沒有例外和錯誤。這裏究竟有什麼錯誤?我如何解決這個問題?提前致謝。

+0

此處顯示的代碼是全表模型嗎?因爲你沒有使用'data'變量。將'data'添加到該變量不起作用 – Robin

+0

爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –

回答

2
  1. 爲什麼原因是有​​

  2. DefaultTableModel可以添加Object[]Vector

  3. 必須重寫AbstractTableModel,而不是DefaultTableModel,必須重寫所有get/set methods,在方法正確fireXxxXxx() ,否則沒有可見的東西(JTable

  4. 可以開始List Table ModelRow Table Model

0

我建議你一個完整的表格模型爲例,它已瞭解如何工作的。它還使用列表作爲數據。最重要的是,您需要擴展AbstractTableModel以使用您自己的變量來存儲數據。這裏是一個完整的源代碼示例。

import java.util.ArrayList; 
import java.util.List; 

    import javax.swing.table.AbstractTableModel; 

    public class MouseClickTableModel extends AbstractTableModel { 
     /** 
     * 
     */ 
     private static final long serialVersionUID = -1807522184370383957L; 

     private final String[] columnNames = { "Sr", "X", "Y", "Delay (ms)", 
       "Comment" }; 

     public final Class[] mColTypes = { Integer.class, Integer.class, 
       Integer.class, Integer.class, String.class }; 

     private final List<MouseClick> data; 

     public MouseClickTableModel(){ 
      data = new ArrayList<MouseClick>(10); 
     } 

     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) { 
      final MouseClick currentRow = (MouseClick) data.get(row); 

      switch (col) { 
      case 0: 
       return currentRow.getSequNb(); 
      case 1: 
       return currentRow.getXcoo(); 
      case 2: 
       return currentRow.getXycoo(); 
      case 3: 
       return currentRow.getDelay(); 
      case 4: 
       return currentRow.getComment(); 
      } 

      return new String(); 
     } 

     public Class getColumnClass(int c) { 
      return mColTypes[c]; 
     } 

     /* 
     * Don't need to implement this method unless your table's editable. 
     */ 
     public boolean isCellEditable(int row, int col) { 
      return false; 
     } 

     public void updateRow(Object value, int row, int col) { 

     } 

     /* 
     * Don't need to implement this method unless your table's data can change. 
     */ 
     @Override 
     public void setValueAt(Object value, int row, int col) { 
      MouseClick currentRow = null; 
      if (row >= data.size()) { 
       // new data 
       currentRow = new MouseClick(); 
       data.add(0, currentRow); 
      } 
      // update row 
      else { 
       currentRow = (MouseClick) data.get(row); 
      } 

      switch (col) { 
      case 0: 
       currentRow.setSequNb(((Integer) value).intValue()); 
       break; 
      case 1: 
       currentRow.setXcoo(((Integer) value).intValue()); 
       break; 
      case 2: 
       currentRow.setXycoo(((Integer) value).intValue()); 
       break; 
      case 3: 
       currentRow.setDelay(((Integer) value).intValue()); 
       break; 
      case 4: 
       currentRow.setComment(((String) value).toString()); 
       break; 
      } 
      // update 
      fireTableCellUpdated(row, col); 
     } 

     public MouseClick getData(int row) { 
      return data.get(row); 
     } 

     public void addMouseClick(MouseClick mc) { 
      insertMouseClick(getRowCount(), mc); 
     } 

     public void insertMouseClick(int row, MouseClick mc) { 
      data.add(row, mc); 
      fireTableRowsInserted(row, row); 
     } 

     public void removeMouseClick(int row) { 
      data.remove(row); 
      fireTableRowsDeleted(row, row); 
     } 

    } 

然後你只需要用你的UI設計更新你的模型。

JTable table = new JTable(new MouseClickTableModel()); 
table.setPreferredScrollableViewportSize(new Dimension(500, 70)); 
table.setFillsViewportHeight(true); 
MouseClickTableModel model = getTable().getModel()); 
model.insertMouseClick(0, new MouseClick(0, Integer.valueOf(xValue), Integer.valueOf(yValue), 2000, "comment")); 
相關問題