How do I get a JTable header to display entire column names instead of shortening them?
Asked Answered
S

3

5

I've a JTable that I fill with a class that extends AbstractTableModel. The problem is that the columns are too short to display the column name. I've turned off Jtable AutoResizeMode property, but shortening still occurs. How do I solve this?

Subterranean answered 31/8, 2011 at 14:58 Comment(0)
A
2

Setting off autoresizemode only prevent the JTable's coumns to be resized. To force a particular width retrieve the table column model:

TableColumn column = jtable.getColumnModel().getColumn(col_num);

and set the desired width:

column.setPreferredWidth(width);
Alesiaalessandra answered 31/8, 2011 at 15:16 Comment(1)
that's only half of the solution: the other half is to find out what the "desired width" is ;) see Rob's answerZadazadack
T
3

You can use the Table Column Adjuster to adjust the column widths based on the header text.

The getColumnHeaderWidth(...) shows how to calculate the actual width using the header text and renderer.

Tureen answered 1/9, 2011 at 0:10 Comment(0)
C
2

there are two ways

1) calculate PreferredSize from TableColumn (by defalut returns JLabel), there are 3-4 correct ways how to determine displayed TextLenght in pixex (please add plus 5-8 pixes moreover)

2) setPreferredSize directly, e.g. TableColumn#setPreferredSize

Cissie answered 31/8, 2011 at 15:15 Comment(1)
randomly adding the one or other pixel or dollar or apple ;-) Have to admit, that SwingX' ColumnFactory does exactly the same, adding a magic "default margin"Zadazadack
A
2

Setting off autoresizemode only prevent the JTable's coumns to be resized. To force a particular width retrieve the table column model:

TableColumn column = jtable.getColumnModel().getColumn(col_num);

and set the desired width:

column.setPreferredWidth(width);
Alesiaalessandra answered 31/8, 2011 at 15:16 Comment(1)
that's only half of the solution: the other half is to find out what the "desired width" is ;) see Rob's answerZadazadack

© 2022 - 2024 — McMap. All rights reserved.