简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM