File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Column sizing in JScrollPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Column sizing in JScrollPane" Watch "Column sizing in JScrollPane" New topic

Column sizing in JScrollPane

Derek Drever

Joined: Mar 01, 2002
Posts: 18
Hi Guys,
If any of you could help I would be grateful:
I have an application with a fixed window size, within this I have a JTabbedPane, each tab contains a JScrollPane which contains a JTable. The tables can have different numbers of columns. I am trying to achieve a uniform look and this is where I am having problems.
I set the columns to a minimum width. If there are few columns, less than the width of the JScrollPane, I would like the columns to resize to fill the scroll pane. This is acheivable by setting the auto resize mode to AUTO_RESIZE_ALL_COLUMNS.
If there are many columns I would like a horizontal scroll bar, this is achieved by setting the auto resize mode to AUTO_RESIZE_OFF, however to achieve both automatically is the problem.
You can achieve this effect by placing the table in a JPanel and adding the panel to the scroll pane but then you lose the column headings.
I would settle for being able to detect the size of the viewport, then I could calculate the minimum column size * the number of columns and set the resize mode accordingly. However when I get the viewport associated with the scroll pane I canot get its size (I used viewport.getExtentSize()), it always comes back with a width and height of 0.
Any ideas would be appreciated.
Derek Drever
Derek Drever

Joined: Mar 01, 2002
Posts: 18
Found a solution on another forum, just extend JTable and override the following method - works a treat.
I agree. Here's the link:
subject: Column sizing in JScrollPane
jQuery in Action, 3rd edition