Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JTable and optimal column width.

Reply
Thread Tools

JTable and optimal column width.

 
 
Christian-Josef Schrattenthaler
Guest
Posts: n/a
 
      07-23-2006
Hi!

I have a JTable which has to columns filled over an 'String[][]'. Is it
possible, to set the cell width to the longest value in an column?

I found a solution with String.lengt() over Google, but this doesn't work
correctly because the value of String.length() doesn't fit exactly. I think
this is because the proportional fonts.

Greetings,
Christian.
 
Reply With Quote
 
 
 
 
Thomas Hawtin
Guest
Posts: n/a
 
      07-23-2006
Christian-Josef Schrattenthaler wrote:
>
> I have a JTable which has to columns filled over an 'String[][]'. Is it
> possible, to set the cell width to the longest value in an column?


Briefly having a look at some of my old code, I've used
JTable.prepareRenderer on TableColumn.getCellRenderer. Then it's a
matter of setting the column preferred width to the maximum of the
preferred widths given by the renderer component. You probably want to
consider the header as well.

> I found a solution with String.lengt() over Google, but this doesn't work
> correctly because the value of String.length() doesn't fit exactly. I think
> this is because the proportional fonts.


String length will give you the number of characters in the text. If you
set the width to one pixel per character, it's probably going to be a
tad short.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
 
 
 
IchBin
Guest
Posts: n/a
 
      07-23-2006
Christian-Josef Schrattenthaler wrote:
> Hi!
>
> I have a JTable which has to columns filled over an 'String[][]'. Is it
> possible, to set the cell width to the longest value in an column?
>
> I found a solution with String.lengt() over Google, but this doesn't work
> correctly because the value of String.length() doesn't fit exactly. I think
> this is because the proportional fonts.
>
> Greetings,
> Christian.

If I understand your problem. Here is a method that will expand the
columns to the largest cell\column..

public void calcColumnWidths(JTable table)
{
JTableHeader header = table.getTableHeader();
TableCellRenderer defaultHeaderRenderer = null;

if (header != null)
defaultHeaderRenderer = header.getDefaultRenderer();

TableColumnModel columns = table.getColumnModel();
TableModel data = table.getModel();
int margin = columns.getColumnMargin(); // only JDK1.3
int rowCount = data.getRowCount();
int totalWidth = 0;

for (int i = columns.getColumnCount() - 1; i >= 0; --i)
{
TableColumn column = columns.getColumn(i);
int columnIndex = column.getModelIndex();
int width = -1;

TableCellRenderer h = column.getHeaderRenderer();

if (h == null)
h = defaultHeaderRenderer;

if (h != null) // Not explicitly impossible
{
Component c = h.getTableCellRendererComponent(table, column
.getHeaderValue(), false, false, -1, i);
width = c.getPreferredSize().width;
}

for (int row = rowCount - 1; row >= 0; --row)
{
TableCellRenderer r = table.getCellRenderer(row, i);
Component c = r.getTableCellRendererComponent(table, data
.getValueAt(row, columnIndex), false, false,
row, i);
width = Math.max(width, c.getPreferredSize().width);
}

if (width >= 0)
column.setPreferredWidth(width + margin); // <1.3:
without margin
else
totalWidth += column.getPreferredWidth();
}
}

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
__________________________________________________ ________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to detect a sync and start of a frame in an optimal way VIPS VHDL 6 06-18-2010 08:18 PM
Re: python and filter design: calculating "s" optimal transform Terry Reedy Python 1 06-02-2010 03:14 PM
Putting a JTable inside a JTable cell? Tivo Escobar Java 1 04-12-2007 11:09 AM
How to move data from a CSV file to a JTable, and from a JTable to a CSV file ? Tintin92 Java 1 02-14-2007 06:51 PM
Adjust column widths of JTable Gerhard Rapp Java 0 07-18-2003 02:58 PM



Advertisments