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