• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to set column width in JTable ?

 
Rite Sara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using JTable with 3 columns in my app. I want to set preferred width to only first column. And the table columns should be resizable.
I don't want to do AUTO_RESIZE_OFF .When a table is rendered it should be displayed with the set width for the first column but the user should be able to chage width afterwords.

 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TableColumn allows you to set many widths, including the preferred width. To give it a fixed size simply give it equal minimum and maximum widths.
 
Rob Camick
Ranch Hand
Posts: 2565
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the JTable API and follow the link to the Swing tutorial on "How to Use Tables" for an example of changing the columns widths.
 
Scott Cook
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AUTO_RESIZE_OFF is necessary, otherwise Java will set the column size for you.
Don't worry, the user can still set the column size later if they want to.

Check out: (sorry link was wrong before - its just what Rob Camick was talking about)
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

It actually has a whole section on how to set column width.

There is even a setResizable() method in the TableColumn class that you can use to keep your first column fixed.
Should be easy!
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out Rob Camick's Table Column Adjuster.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott Cook wrote: AUTO_RESIZE_OFF is necessary, otherwise Java will set the column size for you.

It's only necessary if you want to have full control over all columns. Since only the first column should have the preferred (and fixed?) size set, the remaining columns can be adjusted automatically so AUTO_RESIZE_OFF isn't necessary.
 
Scott Cook
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
Scott Cook wrote: AUTO_RESIZE_OFF is necessary, otherwise Java will set the column size for you.

It's only necessary if you want to have full control over all columns. Since only the first column should have the preferred (and fixed?) size set, the remaining columns can be adjusted automatically so AUTO_RESIZE_OFF isn't necessary.


Rob, you're right. By setting maxWidth and minWidth, he should be able to control the first column without AUTO_RESIZE_OFF (I've never tried it that way).
I suppose I spoke without researching it fully.
The point I was trying to make is that AUTO_RESIZE_OFF doesn't keep the user from being able to adjust the column size. It merely makes it so that
the columns don't resize every time the window size changes (which I find annoying, I would much rather use scroll bars to handle that).

Thanks for correcting my mistake
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic