Search code examples
javaswingjtabledefaulttablemodel

Adding rows in to jtable by customized table model


I Have created a table model extending DefaultTableModel.

public class TableModel extends DefaultTableModel {
List<VariableDetails> data;
public AttachedVariableTableModel(){
super();        
this.data=Collections.synchronizedList(new ArrayList<VariableDetails>());
}

//method for adding rows
public void addRow(VariableDetails varDetails){

    this.data.add(varDetails);
    fireTableRowsInserted(this.data.size()-1,this.data.size()-1);
    }

}

I tried to add rows to the table which has already data in it.

tModel.addRow(new  VariableDetails());

but rows cannot be added. There is no exceptions and errors. what is actually wrong here? How Can i Solve this? Thanks in advance.


Solution

    1. for why reason is there super();

    2. DefaultTableModel can add Object[] or Vector

    3. have to override AbstractTableModel, rather than DefaultTableModel, have to override all get/set methods, with proper fireXxxXxx() in the methods, otherwise there aren't something visible in view (JTable)

    4. can start with List Table Model or Row Table Model