Adi Mor
Adi Mor

Reputation: 2165

JTable - why doesn't it keep the TableColumns after adding a row?

I'm using a JTable :

public class MyPanel extends JPanel {

public MyPanel () {
   init()
}


init () {
DataModel model = new DataModel();
JTable table = new JTable (model);

TableColumnModel tcm = agentsTable.getColumnModel();
        tcm.getColumn(FIRST).setPreferredWidth(15);
        tcm.getColumn(SECOND).setPreferredWidth(100);
        tcm.getColumn(THIRD).setPreferredWidth(200);


        JScrollPane scroller = new JScrollPane();
        scroller.getViewport().add(table);
        this.add(scroller, BorderLayout.CENTER);
}

}


public class DataModel extends AbstractTableModel {

public final static int FIRST = 0;
public final static int SECOND = 1;
public final static int THIRD= 2;

...

}

the problem is every time i add a row to the model, the TableColumnModel seems to go back to default, and all the columns have the same width

Upvotes: 1

Views: 68

Answers (1)

kleopatra
kleopatra

Reputation: 51525

bet your custom TableModel fires a structureChanged on insert :-) That's incorrect, instead use fireTableRowsInserted().

Upvotes: 2

Related Questions