Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JTable单元格不反映更改,但可编辑_Java_Swing_Model_Jtable - Fatal编程技术网

Java JTable单元格不反映更改,但可编辑

Java JTable单元格不反映更改,但可编辑,java,swing,model,jtable,Java,Swing,Model,Jtable,对于自定义表格模型,我覆盖了isCellEditable,它总是返回true 我还重写了setValueAt,但不知道如何使用该方法,因此,JTable反映了编辑所做的更改 以下是PersonTableModel的修改代码:- class PersonTableModel extends AbstractTableModel{ public int getRowCount(){ return 10 ; } public int getColumnCou

对于自定义表格模型,我覆盖了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
    }
}
问候,, 里茨


编辑:

根据表单成员的建议,下面是我使用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(); 
}
类PersonTableModel扩展了AbstractTableModel{
字符串[]数据;
//我将添加一个构造函数,它首先用
//您想要的值。如“Person 1”“Person 2”等。
//您还可以考虑在此处传递一个大小值,该值决定
//表的容量,因此也包括表中的行。
//(但这需要您更改getRowCount方法)。
公共表格模型(){
数据=新字符串[10]

对于(int i=0;iI)已将“类PersonTableModel扩展AbstractTableModel”更改为“类PersonTableModel扩展DefaultTableModel”,并且还从我的PersonTableModel类中删除了setValueAt方法。但是,更改也没有得到反映。然后,您的代码出现了另一个问题。这就是为什么您应该从创建SSCCE()开始的原因。然后,如果您有问题,您可以发布SSCCE。我已经尝试了,正如您所建议的,但是当我编辑它时,单元格没有得到更新。您没有使用DefaultTableModel。默认情况下,所有单元格都是可编辑的,因此您甚至不需要像我之前建议的那样覆盖isCellEditable()方法。而且您仍然没有发布SSCCE,因此我无法提供帮助。