Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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);  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top