0

Can someone show me why my JTable is not updating with my model below? It comes up fine the first time but when I enter new data being held in the arrays int nh, string hNam, int vh, string proc_1 , string proc_2, the table won´t update with the new data.

public class TableModel extends AbstractTableModel {

    int numRows;
    String columnNames[] = {"NH", "Horse Names", "VH",
        "Proc. I", "Proc. II"};
    Object[][] myData;

    TableModel() {
        super();
        numRows = fnh;
        myData = new Object[fnh][5];

        for (int i = 0; i < fnh; i++) {
            for (int j = 0; j < 5; j++) {
                if (j == 0) {
                    myData[i][0] = (Integer) nh[i];
                } else if (j == 1) {
                    myData[i][1] = (String) hNam[i];
                } else if (j == 2) {
                    myData[i][2] = (Integer) vh[i];
                } else if (j == 3) {
                    myData[i][3] = (String) proc_1[i];
                } else {
                    myData[i][4] = (String) proc_2[i];
                }
            }
        }
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public int getRowCount() {
        return myData.length;
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        return true;
    }

    @Override
    public Object getValueAt(int row, int col) {

        if (col == 0) {
            return nh[row];
        } else if (col == 1) {
            return hNam[row];
        } else if (col == 2) {
            return vh[row];
        } else if (col == 3) {
            return proc_1[row];
        } else {
            return proc_2[row];
        }
    }

    @Override
    public void setValueAt(Object value, int row, int col) {
        myData[row][col] = value;
        fireTableCellUpdated(row, col);
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I don't see your implementation of `getColumnClass()`. Please edit your question to include an [sscce](http://sscce.org/) that exhibits the problem you describe. – trashgod Apr 01 '13 at 05:39

1 Answers1

1

If you are updating a row in JTable and use setValueAt explicitly instead of just changing the value in data. And if you are adding new data to JTable then create a method in TableModel And recreate new data array with size 1 more than existing one fill it up with old data and then add new data to it. And then fire fireTableRowsInserted(int firstRow, int lastRow)

Vishal K
  • 12,976
  • 2
  • 27
  • 38