[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1018
  • Last Modified:

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.

0
richard_fairall
Asked:
richard_fairall
  • 5
  • 4
  • 3
  • +1
3 Solutions
 
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
 
objectsCommented:
you could get the width by grabbing the cell renderer for the cell and looking at its preferred width.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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
 
zzynxSoftware engineerCommented:
thanks
0
 
accarvajalCommented:
Thanks! ;-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now