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 didn’t get the desired output because when I use this I have a horizontal scrollbar to scroll to the columns that are off screen. But I want the size of the whole table to 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?
Unfortunately, the maximum width of the
JTablewill be limited to the size of theJFrame, and there is no mechanism to push from yourJTablethe desired width to theJFramewhen yourJTableis contained in aJScrollPane.What you could do is try to give your
JScrollPanethe correct width. I haven’t tried this, but I suspect that giving your scrollpane the same width as the preferred width of your table (when autoresize is on) should solve this (+ perhaps some extra width for the vertical scrollbar). Note however that this will only work if you allow yourJFramein which theJScrollPaneis contained to grow in width as well.