0

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.

sher17
  • 607
  • 1
  • 12
  • 31
  • I see nothing wrong with the code you included, so the problem is elsewhere. – TT. Mar 09 '16 at 09:17
  • in order to check the method calls I added some print statements but the getValueAt() overridden method is not even getting called. What may be the reason – sher17 Mar 09 '16 at 09:20
  • 1
    @Sherihan How are you adding your table to its scrollpane? If you are using `scrollpane.add` , try replacing it with `scrollpane.setViewportView(table)` – Priyath Gregory Mar 09 '16 at 09:30
  • yes earlier I used the add method to add the table to the scrollpane and changed it now using setViewportView() – sher17 Mar 09 '16 at 09:43

1 Answers1

2

Probably you forgot to include the fact that you need to trigger the view (JTable) to update itself from your Model if the underlying data changes in the Model (using fireTableDataChanged)... I assume that initially your list is empty and data gets added to the list and that dats is not displayed in the JTable.

I can recommend you to have a look at GlazedLists !!! (http://www.glazedlists.com/)

This freeware library suits your needs perfectly !

GerritCap
  • 1,606
  • 10
  • 9
  • I declared the propertiesTable variable as a static class variable and it worked out. – sher17 Mar 09 '16 at 09:45
  • @Sherihan `static` is a poor choice and should be used sparingly as it can lead to no end of strange and wonderful issues which are difficult to diagnose and tends to be a sign of a poor design – MadProgrammer Mar 09 '16 at 10:54
  • In mt swing application when the app loads, initially I display the table with the column names. I have several layers of panels and a JScrollPane holding the table. Once the user clicks on a node, then the table should display some data. then the same table needs to be updated. So, I thought making the Table static is fine. According to your opinion do you have any other suggestions to resolve this? – sher17 Mar 09 '16 at 10:58
  • You're still missing a point. It is not because your attribute fieldList get's populated that suddenly the entries in that list will be displayed. When the TableModel is used to display the JTable, the JTable request the number of rows in your model, which I assume is 0 at that time. You need to force the JTable to enquire the data again -> check the fireTableDataChanged methods on table model. As I said earlier, GlazedLists does this all for you. The EventList signal the TableModel that data has been added/removed,... – GerritCap Mar 14 '16 at 18:36