TheGuiMarks TheGuiMarks - 1 year ago 232
Java Question

Java - Only Vertical Scroll

I'm trying to create a Character Sheet from GURPS and it's basically like this:

/-Main Panel--------\
|/-Another Panel---\|
|| Contents ||

And I need it to scroll vertically, but to resize horizontally. I'm using BoxLayout to arrange the panels vertically, a personal layout to fit entirely the contents in his container, and tried to use:

JScrollPane scrollPane = new JScrollPane(mainPanel);

But it didn't work as I wanted, it doesn't change the horizontal size of it's contents. Does anyone have any ideas?

Answer Source

By default the component added to a JScrollPane is displayed at its preferredSize() and scrollbars appear when the preferred size is greater than the size of the scroll pane.

If you want to force the panel to expand/shrink horizontally to fill the width of the scroll pane then you need to implement the Scrollable interface for your panel.

So basically you would need to implement the getScrollableTracksViewportWidth() method to return “true”. Now the main panel will be resized to fill the width of the scroll pane.

Or instead of implementing all the methods of the Scrollable interface yourself you can check out the Scrollable Panel which provides this support for you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download