aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to set column width in JTable ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to set column width in JTable ?" Watch "How to set column width in JTable ?" New topic
Author

How to set column width in JTable ?

Rite Sara
Ranch Hand

Joined: Feb 13, 2010
Posts: 56
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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
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

Joined: Aug 01, 2006
Posts: 14
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

Joined: May 03, 2008
Posts: 4638
    
    5

Check out Rob Camick's Table Column Adjuster.


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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

Joined: Aug 01, 2006
Posts: 14
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to set column width in JTable ?