I am trying to resize the widths of the columns in a JTable.
I would need to resize them when the program is running to see the full column header names.
I want to resize table of database showing in swing frame.

I have tried

table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS)

But i dont get the desired output becouse when i use this i have a horizontal scrollbar to scroll to the columns that are off screen. But i want that the size of the whole table should become approximately equal to the size of frame.

How can I make the columns as wide as they need to be to display all the text?
Please answer as soon as possible.

Recommended Answers

All 4 Replies

You seem to be asking for two things:
1. Make the table wide enough to show all the text
2. Make the table fit in the frame without scrolling
You can't have both when the text is long, so what is your single requirement?

I want to make the table wide enough to show whole text without scrolling when text is not too long.

AUTO_RESIZE_ALL_COLUMNS does that anyway. But what do you want to do when the text is too long?

I have tried this already

table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS)

Also my text is not too long. I just want to display the table to full dimension of the frame (width). I don't want to use any scroll bar at all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.