Skip to main content

How to determine column width when using Highlighters?

6 replies [Last post]
uvoigt
Offline
Joined: 2006-01-26
Points: 0

I want to set the width of one column in a JXTreeTable to to width of the largest cell in this column.
I tried to do it as follows:

int maxWidth = 0;
for (Object cellvalue : colValues)
{
  Component rend = unitColumn.getCellRenderer().getTableCellRendererComponent( table, cellvalue, true, true, 0, 0);
  if (rend.getPreferredSize().width > maxWidth)
  {
       maxWidth = rend.getPreferredSize().width;
  }
}

unitColumn.setMaxWidth(maxWidth);
unitColumn.setMinWidth(maxWidth);

This works well unless I am using highlighters.

Two questions:
* Is there an easier method to do this?
* How can I include the highlighters in my width calculation?
Thanks!
Uli

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kschaefe
Offline
Joined: 2006-06-08
Points: 0

Highlighters that install icons, change font sizes, or alter borders could affect the size of the cell. You should be able to call table.prepareRenderer to obtain the fully highlighted renderer.
Karl

uvoigt
Offline
Joined: 2006-01-26
Points: 0

Thanks Karl,
it's working for me.
Uli

kleopatra
Offline
Joined: 2003-06-11
Points: 0

hmm ... cant imagine how Highlighters would interfere: typically, they shouldn't touch any size related properties.
Small example please?
Cheers
Jeanette

uvoigt
Offline
Joined: 2006-01-26
Points: 0

1. Change the font (Courier is wider than Arial)
2. Change the font size (very simple ;-)
3. Bold fonts are often wider than the normal variant
...

Uli

kleopatra
Offline
Joined: 2003-06-11
Points: 0

;-)
Alternatively, use table.packColumn:
table.packColumn(colIndex, 0);
TableColumn column = table.getColumn(colIndex);
column.setMinSize(col.getPreferredSize());
column.setMaxSize(col.getPreferredSize());
CU
Jeanette

uvoigt
Offline
Joined: 2006-01-26
Points: 0

That really sounds too simple .... but it works.
I prefer the other solution because I additionally check if there is any data in the column. If not I can automatically hide it.
Thanks!
Uli