JTable - 为什么添加一行后不保留 TableColumns?
我正在使用 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 似乎都会返回默认值,并且所有列都具有相同的宽度
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
打赌您的自定义 TableModel 会在插入时触发 StructureChanged :-) 这是不正确的,而是使用 fireTableRowsInserted()。
bet your custom TableModel fires a structureChanged on insert :-) That's incorrect, instead use fireTableRowsInserted().