开发者

Updating number of columns in TableModel

开发者 https://www.devze.com 2023-04-10 01:10 出处:网络
i use a DataModel which extends an AbstractTableModel for my Jtable. When i use the constructor JTable main = new JTable(model);

i use a DataModel which extends an AbstractTableModel for my Jtable. When i use the constructor JTable main = new JTable(model); everything works fine but when i add columns to my DataModel and call this.fireTableStructureChanged() the number of columns does not update as can be seen in the following example.

Updating number of columns in TableModel

It shows a JTable initialised with my DataModel, which contained 3 rows and 3 columns. The DataModel has been updated to 4 rows and 4 columns but does only show 3 colu开发者_如何学Cmns.

My Changeevent seems to be wrong because:

JTable.getColumnCount() shows 3
but DataModel.getColumnCount() shows 4

How can i tell the Table that the number of columns has changed and it should repaint them?


The JTable also has a TableColumnModel which holds all the columns to show. When you add another column to your model, the column model still has the old number of columns unless the table's autoCreateColumnsFromModel property is set to true (table.setAutoCreateColumnsFromModel(true)).

The other way would be to manually add another column to the column model.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号