简体   繁体   中英

JTable cell not reflecting changes, though editable

For a custom TableModel, I am overriding isCellEditable , which is returning true always.

I am also overriding setValueAt , but don't know how to use the method, so that, JTable reflects the changes done by editing.

Below is the modified code for 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
    }
}

Regards, Rits


Edit:

As suggested by form members, below is the code where I am using 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() ;
    }
}

Extend the DefaultTableModel and then you only need to override the isCellEditable(...) method. The default table model already implements the setValueAt() method in addition to other usefull methods.

If you really want to know what goes in the setValueAt(...) method, then look at the source code for the DefaultTableModel to see how setValueAt() notifies the view that the model has changed by invoking the appropriate fireXXX method.

Your first version of the PersonTableModel already is very close i would say. I assume you want to have a table with one column with header "Name" and then in each row a name which is editable.
The reason why your changes dont get displayed is because you dont have a underlying data structure for saving them. I would suggest adding a String Array to save the names. Then the code for your TableModel should look like this:

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

}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM