簡體   English   中英

盡管可編輯,但JTable單元格沒有反映變化

[英]JTable cell not reflecting changes, though editable

對於自定義TableModel,我重寫isCellEditable ,它總是返回true。

我也重寫了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() ;
    }
}

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

如果您真的想知道setValueAt(...)方法的內容,那么請查看DefaultTableModel的源代碼,了解setValueAt()如何通過調用相應的fireXXX方法通知視圖模型已更改。

我會說你的第一個版本的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(); 
}

}

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM