I've a Jtable in which i'm setting data using setDataVector function.It's working fine but suppose if i have changed some column width simply by dragging using mouse and after that new data loads in the same table then the width of that column reset to it's default position.So basically i want to retain that width even if new data loads.

有帮助吗?

解决方案

When you use the setDataVector() method the structure of the table may change so the table automatically recreates the TableColumnModel and you lose your customization.

When you create the JTable the first time use code like:

JTable table = new JTable(...);
table.setAutoCreateColumnsFromModel( false );

to prevent the TableColumnModel from being recreated.

其他提示

That happens because when you use setDataVector() according to docs that recreates columns and your column width changes to default.

Try to use setAutoCreateColumnsFromModel() to prevent that behavior.

As you use a custom table model. Don't use setDateVector() it recreates the columns. You can manage width of column using setPreferredWidth() method manually as:

      table.setModel(buildTableModel(rs));
      TableColumnModel tcm = table.getColumnModel();
      tcm.getColumn(0).setPreferredWidth(80);  
      tcm.getColumn(1).setPreferredWidth(200);  
      tcm.getColumn(2).setPreferredWidth(100);  
      tcm.getColumn(3).setPreferredWidth(100);  
      tcm.getColumn(4).setPreferredWidth(100);  
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top