• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting column width in JTable

 
Raja Kannappan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've five columns in my JTable. Currently all of them are of same width. I want to set my first column to be of less width than the other four columns. How do I set it?
I only see a method called setRowHeight() in JTable but there is no method to set a column width.
Thanks,
- Raja.
 
Jeremy Maddera
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A possible solution to your issue is use a TableColumnModel this allows you to set the width of each column.
The Constructor for TableColumn allows you to set the width....

you also need the set the setAutoResizeMode on the Table object.
 
Raja Kannappan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll try that.
Thanks,
- Raja.
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you don't need to create the table columns yourself:
 
Raja Kannappan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
myTable.getColumnModel().getColumn(0).setMaxWidth(int);
This one worked fine.
Thanks a lot!!!
- Raja.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic