Ervinas34
Ervinas34

Reputation: 133

Java Swing JTable column value and header full width, how is it possible?

Trying to make my JTable column width to be auto resized to it's value width.

for example i have a column1 with text "just a test" and i get this result :

|column1|
|-------|
|just ..|

but i need it to be like so:

|column1    |
|-----------|
|just a test|

I've already searched whole JTable library for this one, can't find anything related. Any help?

Upvotes: 0

Views: 759

Answers (1)

Jorge Hernandez
Jorge Hernandez

Reputation: 106

First answer

To do this you should access the column of the table as this:

TableColumn column = null; column = table.getColumnModel().getColumn(/*here the index of your column*/); column.sizeWidthToFit();

be careful if header is empty or it will do nothing

some of the sources just in case https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#width https://docs.oracle.com/javase/7/docs/api/javax/swing/table/TableColumn.html#sizeWidthToFit()

Update

after some more search I found this previous post that should be a better answer to your problem Auto resizing the JTable column widths

Upvotes: 1

Related Questions