0

I have a problem with refreshing a JTable. I start with an empty ArrayList and after setting my choice of a combo box I load content to the ArrayList but JTable does not react to it - it remains empty. Is it a problem with a TableModel?

This is my code...

public class ProjectTableModel extends AbstractTableModel{

    private static final long serialVersionUID = 1L;

    private static ArrayList<String> caseList = new ArrayList<String>();

    @Override
    public int getColumnCount() {
        return 1;
    }

    @Override
    public int getRowCount() {
        return caseList.size();
    }

    @Override
    public Object getValueAt( int row , int col) {
        getRowCount();
        switch(col){
            case 0:
                System.out.println("mam to");
                return caseList.get(row);
        }   
        return null;            
    }


    public void setCaseList(String[] list){
        for (int i =list.length - 1; i >= 0; i--) {
            caseList.add(list[i]);
        }
        System.out.println(getRowCount());
        fireTableDataChanged();
    }

    public String setValueAt(int row, int col){ 
        return null;
    }

    public void addTestCase(String name) throws IOException{    
        File newDir = new File(TestCaseMaker.currentDir, 
            "Przypadek testowy"+(caseList.size()+1));
        newDir.createNewFile();
        caseList.add(name);

        fireTableDataChanged();
    }

    public String getColumnName(int col) {
        return "Przypadki testowe";
    }
}
wattostudios
  • 8,666
  • 13
  • 43
  • 57
Swemack
  • 11
  • 3

1 Answers1

2

Your implementation of setValueAt() is incorrect. It has the wrong signature, and it fails to notify its view.

Addendum: My JTable does not react to any changes to data model

For reference, EnvTableTest is an example using AbstractTableModel that precludes editing; the default implementation of isCellEditable() always returns false.

@Override
public void setValueAt(Object aValue, int row, int col) {
    if (col == 1) {
        System.out.println("setValueAt: " + row + " " + aValue);
        // update caseList here
        this.fireTableCellUpdated(row, col);
    }
}

There's a related example here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Just out of curiosity ... does the implementation of this method matters if you have a non-editable table model (non-editable as in no editor on the `JTable`). I would think this method is not called at all in that situation – Robin Jun 20 '12 at 08:27
  • Even though my JTable does not react to any changes to data model(changes of contents in ArrayList). I used a similar model found on the net in other project and it worked perfectly... – Swemack Jun 20 '12 at 08:44