views:

39

answers:

2

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() ;
    }
}
+1  A: 

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.

camickr
@camickr : I have changed "class PersonTableModel extends AbstractTableModel" to "class PersonTableModel extends DefaultTableModel", and also removed setValueAt method from my PersonTableModel class. But, then also, changes are not getting reflected.
rits
Then you have another problem with your code. That is why you should start by creating a SSCCE (http://sscce.org). Then you can post the SSCCE if you have problems.
camickr
@camickr : I have updated the post, as u suggested. :-)
rits
I have tried, what u suggested, but cell is not getting updated, when I edit it.
rits
You are not using the DefaultTableModel. By default all cells are editable, so you don't even need to overide the isCellEditable() method as I suggested earlier. And you still haven't posted a SSCCE so I can't help.
camickr
A: 

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.

dennisg