0

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;
    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
user2100513
  • 101
  • 8
  • What does `Section` look like? Also, `getValueAt` should be return the value for specified cell (row/column) not the object which represents the row... – MadProgrammer Jun 16 '15 at 01:23
  • Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Jun 16 '15 at 02:04
  • "*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."* - `JTable` is heavy optimised to reduce overheads of painting large number of rows. It would be impossible not to paint the viewable area, in which the updates have occurred, as this is how painting works. What it tries to do is not paint areas of the table which aren't visible – MadProgrammer Jun 16 '15 at 02:06
  • Thank you for your reply...I will try to post a runnable example...The actual value gets printed by implementing TableCellRenderer where the value gets calculated and its life span is only for that particular iteration...But i wanted it to stay printed on table for users view.... – user2100513 Jun 16 '15 at 12:44
  • As per your third comment...is there a way to know if the row is already printed then not to repaint it again ? – user2100513 Jun 16 '15 at 12:45
  • No. You `TableModel` MUST make available the live data that is to be made available to view – MadProgrammer Jun 16 '15 at 20:39

2 Answers2

0

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);
}
serg.nechaev
  • 1,323
  • 19
  • 26
  • So you're just ignoring what's passed to the method? – MadProgrammer Jun 16 '15 at 01:49
  • This is to test if the original code doesn't change the object after it is added. – serg.nechaev Jun 16 '15 at 01:58
  • So, in other words, it doesn't "answer" the question, but is asking for clarification of the problem ... – MadProgrammer Jun 16 '15 at 02:01
  • The "answer" is that the objects are not being managed properly. To confirm it - the code snippet is attached. It can be a wrong answer, but without the context - a good one. – serg.nechaev Jun 16 '15 at 02:04
  • Thanks for the reply...This is how i pass the instance.. RunConfig = new RunConfig(); Section sectionToAdd = (Section) cobSections.getSelectedItem(); RunConfig.addElement(sectionToAdd); – user2100513 Jun 16 '15 at 12:41
0

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.

user2100513
  • 101
  • 8