1

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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
avinash
  • 163
  • 2
  • 12

3 Answers3

7

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks for the help ,I have tried to use setAutoCreateColumnsFromModel() but after this change i'm getting a blank table.Also i'm using a custom table model for Jtable. – avinash Jan 13 '14 at 08:17
  • @avinash, `i'm getting a blank table.` its a simple solution, but since you still haven't accepted answers from any of your previous questions I'm not about to spend time helping someone who doesn't appreciate the help. – camickr Jan 13 '14 at 16:29
  • oh sorry i was not aware of this " accepting answer" thing.I'll try to take care of this in the future. – avinash Jan 13 '14 at 19:09
  • @avinash, make sure you add the TableModel to the table before using the method I suggested otherwise the table will contain 0 columns. – camickr Jan 13 '14 at 20:18
1

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.

alex2410
  • 10,904
  • 3
  • 25
  • 41
0

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);  
ravibagul91
  • 20,072
  • 5
  • 36
  • 59