• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to set column width in JTable ?

 
Rite Sara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out Rob Camick's Table Column Adjuster.
 
Rob Spoor
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic