JTable column minimum widths set to largest cell item or column header width

Hi

I have a JTable which can be created with anything from1 to 100 columns.  
When there are a lot of columns I would like to have the minimum size
of each column set to the greater of a columns header size or the largest
cell size in that column.
I'll just rephrase this to make sure I understand myself:
Each column must size itself individually to a minimum width dictated by
the larger of:  The width of the column header or the largest item in the column.
I always want to see the full column header and scroll horizontally as necessary.
Currently (by default I assume) my table shares the widths of all columns reducing them
to unreadable pencil-widths.

Phew!
Thanks.

richard_fairallAsked:
Who is Participating?
 
accarvajalCommented:
I recommend you take a look at:

http://www.javaalmanac.com/egs/javax.swing.table/Pack.html?l=rel

here, is the way to pack all columns width.

http://www.javaalmanac.com/egs/javax.swing.table/PackCol.html

here, is the way to pack only a column width

I used them and perfect!, It worked for me!
0
 
objectsCommented:
You need to go thru the rows of the table looking at the width of each column mnually afaik
0
 
richard_fairallAuthor Commented:
Thanks

Oh Bu**er, I thought it was all built in to JTableModelSelectionColumnHeaderHerbert.

I suppose I could :
1. Infiltrate getValueAt() in the model and find and save size of columns.
2. Fire a change
3. Adjust size of columns if increased.
4. Hope there are no visual side-effects.

How do I determine a width of a columns from data which could be String
or Integer, I could convert the latter to String but,
Is there an easy method to determine column width from data size?

Rich
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
objectsCommented:
you could get the width by grabbing the cell renderer for the cell and looking at its preferred width.
0
 
richard_fairallAuthor Commented:
Ok,
So obviously this cant be done in the model.
I must do it in the controller after a tableChange.
There could be 20,000 rows in this table so
I'm trying to get this done on the basis of updating the
column widths only on the currently visible rows.
Is there a simple way of detemining the visible rows range?

Rich
0
 
zzynxSoftware engineerCommented:
>> Currently (by default I assume) my table shares the widths of all columns reducing them to unreadable pencil-widths.
Try
        yourTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

0
 
zzynxSoftware engineerCommented:
>> I'm trying to get this done on the basis of updating the
>> column widths only on the currently visible rows.
And then perform that on every scroll up/down?

>> Is there a simple way of detemining the visible rows range?
If there is, it will probably use JTable's rowAtPoint() method
0
 
richard_fairallAuthor Commented:
Hi zzynx
Yes every scroll up or down would not be prohibitively time-consuming, as long as it works.
Setting AUTO_RESIZE_OFF does make a big improvement, although the wider column headers are truncated
and the wider column cells are also truncated (trailing ellipsis).

I'm assuming the getValueAt() method only gets called when updating the visible rows on focus change
or scroll change etc.

The next thing is how to find the Points of the visible rows..
Rich



0
 
zzynxSoftware engineerCommented:
>> Setting AUTO_RESIZE_OFF does make a big improvement
I know
>> although the wider column headers are truncated and the wider column cells are also truncated (trailing ellipsis).
I know ;°)

>> The next thing is how to find the Points of the visible rows
I think soemthing like (0,0) for the upper left corner of the JTable
and (0,0+tableheight) for the lower left corner.

Could you try with (0,0)?
0
 
objectsCommented:
> The next thing is how to find the Points of the visible rows..

You can get the coords from the viewport.

0
 
richard_fairallAuthor Commented:
Thanks both,
Had a meeting!
I'm playing around at the moment with the suggestions, probably
wont get back with a solution until sometime tomorrow.
Rich
0
 
zzynxSoftware engineerCommented:
Take your time ;°)
0
 
zzynxSoftware engineerCommented:
thanks
0
 
accarvajalCommented:
Thanks! ;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.