2010-10-24 74 views
0

對於自定義TableModel,我重寫了isCellEditable,它始終返回true。JTable單元格不反映更改,但可編輯

我也覆蓋setValueAt,但不知道如何使用該方法,以便JTable能夠反映編輯所做的更改。

下面是PersonTableModel修改後的代碼: -

class PersonTableModel extends AbstractTableModel{ 

    public int getRowCount(){ 
     return 10 ; 
    } 

    public int getColumnCount(){ 
     return 1 ; 
    } 

    public String getColumnName(int c){ 
     return "Name" ; 
    } 

    public Object getValueAt(int r, int c){ 
     return "Person " + ++r ; 
    } 

    public boolean isCellEditable(int rowIndex, int columnIndex) { 
     return true ; 
    } 

    public void setValueAt(Object aValue, int rowIndex, int columnIndex) { 
     //what goes here 
    } 
} 

問候, RITS


編輯:

如所建議的通過模板構件,下面是我在哪裏的代碼使用PersonTableModel: -

public class CustomTableModel{ 

    @SuppressWarnings("deprecation") 
    public static void main(String[] args){ 
     JFrame frame = new PersonFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; 
     frame.show(); 
    } 
} 

class PersonFrame extends JFrame{ 

    @SuppressWarnings("deprecation") 
    public PersonFrame(){ 
     setTitle("PersonTable"); 
     setSize(600, 300); 

     TableModel model = new PersonTableModel() ; 
     JTable table = new JTable(model); 

     getContentPane().add(new JScrollPane(table), "Center") ; 
     show() ; 
    } 
} 
+0

Thanx camickr :) – mogli 2010-10-24 19:19:30

回答

1

擴展DefaultTableModel,然後您只需要重寫isCellEditable(...)方法。除了其他有用的方法外,默認的表模型已經實現了setValueAt()方法。

如果您確實想知道setValueAt(...)方法的含義,請查看DefaultTableModel的源代碼,以瞭解setValueAt()如何通過調用相應的fireXXX來通知視圖模型已更改方法。

+0

我已將「PersonTableModel extends AbstractTableModel」更改爲「類PersonTableModel extends DefaultTableModel」,並且還從我的PersonTableModel類中刪除了setValueAt方法。但是,那麼變化也沒有得到體現。 – mogli 2010-10-24 19:25:08

+0

然後你的代碼有另一個問題。這就是爲什麼你應該開始創建一個SSCCE(http://sscce.org)。如果你有問題,你可以發佈SSCCE。 – camickr 2010-10-24 19:51:03

+0

我按照你的建議更新了這篇文章。 :-) – mogli 2010-10-24 20:18:41

1

您的PersonTableModel的第一個版本已經非常接近了,我會說。我假設你想要一個帶有「Name」標題的列的表,然後在每行中有一個可編輯的名稱。
更改不顯示的原因是因爲您沒有底層數據結構來保存它們。我會建議添加一個字符串數組來保存名稱。然後,你的TableModel的代碼應該是這樣的:

class PersonTableModel extends AbstractTableModel{ 

String[] data; 

// I would add a constructor which fills the column initially with the 
// values you want to have. Like "Person 1" "Person 2" and so on. 
// You can also think about passing a size value here which determines 
// the capacity of the table and therefore also the rows in the table. 
// (But this would require you to change the getRowCount method). 
public PersonalTableModel(){ 
    data = new String[10] 
    for(int i = 0; i<10; i++){ 
     data[i] = "Person "+i; 
    } 
} 


public int getRowCount(){ 
    return 10 ; 
} 

public int getColumnCount(){ 
    return 1 ; 
} 

public String getColumnName(int c){ 
    return "Name" ; 
} 

// Since you dont have multiple columns you only need to pass the row here 
public Object getValueAt(int r){ 
    // Simply get the corresponding String out of the data array 
    return data[r]; 
} 

public boolean isCellEditable(int rowIndex, int columnIndex) { 
    return true ; 
} 

// Here you also dont need to pass the column index 
public void setValueAt(Object aValue, int rowIndex) { 
    // Save the new name into the array 
    data[rowIndex] = aValue.toString(); 
} 

}

希望這有助於。