JTable - 添加行后为什么不保留TableColumns?

时间:2011-12-07 14:17:38

标签: swing jtable tablemodel nstablecolumn

我正在使用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;

...

}

问题是每次我向模型添加一行时,TableColumnModel似乎都会返回默认值,并且所有列都具有相同的宽度

1 个答案:

答案 0 :(得分:2)

打赌你的自定义TableModel会在插入时触发structureChanged :-)这是不正确的,而是使用fireTableRowsInserted()