I have a swing application where I mainly have a Jtree and a JTable. when the app starts, the tree shows a list of values and the table only displays its column names.Once a node is selected from the tree, some data related to the selected node needs to be displayed in the table. I have a Table model class which extends the AbstractTableModel as below
import javax.swing.table.AbstractTableModel;
import java.util.List;
public class PropertiesTableModel extends AbstractTableModel{
private List<Field> fieldList;
private final static String[] columnNames= new String[]{"Field","Value"};
public PropertiesTableModel(List<Field> list){
this.fieldList=list;
}
@Override
public String getColumnName(int columnIndex){
return columnNames[columnIndex];
}
@Override
public int getRowCount() {
return fieldList.size();
}
@Override
public int getColumnCount() {
return 2;
}
//this method is called to set the value of each cell
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Field field= (Field) fieldList.get(rowIndex);
switch(columnIndex){
case 0:
return field.getFieldDef().getfName();
case 1:
return field.getDefaultValue();
}
return null;
}
@Override
public Class<?> getColumnClass(int columnIndex){
switch (columnIndex){
case 0:
return String.class;
case 1:
return Object.class;
}
return null;
}
}
from another class I use this table model as below
public void populateTableData(List<Field> list){
this.fieldList=list;
JTable propertiesTable=new JTable();
propertiesTable.setModel(new PropertiesTableModel(fieldList));
}
When I run the application and select a node from the tree, the table is not getting populated with expected data. I made sure that the fieldList data is passed to the TableModel but only the getColumnName() and the getColumnCount() methods are getting called.
I want to know if something is missing in my code.