Java AbstractTableModel Dynamically populating rows

189 views Asked by At

I am trying to add rows dynamically in a table with AbstractTableModel. The rows are being added in a loop. When the second row gets added, the values of the first row show as null in table. The program is designed such a way that the rows values get refreshed but just for the user's viewing sake, how can i keep the values on the table as it is without it being refreshed to null values.

Why does the table always refreshes the table rendering from first row? Is there a way to render only next row without reprinting what has already been printed in the first row.

Here are some parts of my code:

public class RunConfig extends AbstractTableModel {


    private final LinkedList<Section> daten = new LinkedList<Section>();
    private String[]        header = {"Links","% N", "% M", "% H", "% S"};

    public RunConfig() {

    }

    public void addElement(Section addValue)
    {
        daten.add(addValue);
        fireTableRowsInserted(daten.size()-1, daten.size()-1);
    }

    public int getColumnCount() {
        return header.length;
    }

    public String getColumnName(int col) {
        return header[col];
    }


    public Object getValueAt(int row, int col) {
        return ((Section)daten.get(row)); 
    }

    public int getRowCount() {
        return daten.size();
    }

    public boolean isCellEditable(int row, int col){
        return false;
    }

}
2

There are 2 answers

5
serg.nechaev On

I am wondering if the application is managing the Section objects correctly. Please make sure every addElement is working with a different instance of Section object and not reusing the same object or nulling its fields after adding to the table model:

Please try the following to make sure table gets a unique object that is never modified from the outside:

public void addElement(Section addValue) {
    Section sec = new Section();
    // TODO copy fields
    daten.add(sec);
    fireTableRowsInserted(daten.size() - 1, daten.size() - 1);
}
0
user2100513 On

Since i couldn't figure out how to resolve the issue, i used a simple defaultTableModel instead and took the data in an array and populated the table dynamically row by row. Its easy too. Thanks for your help and comments.