JScrollPane Blunders: Part 3 (of 3, phew!)

This is the final “blunder” of this series. In Part 1, I showed how wrapping the entire screen in one giant scroll pane is usually a bad idea. Part 2 showed how nested scroll panes leads to double scrolling or unusual resize behavior. Now, we see that adding a scroll pane to a BorderLayout’s NORTH, SOUTH, EAST, or WEST region can be troublesome.

As usual, a visual example shows the problem. Here we have a scroll pane surrounding a JTree. This is placed in BorderLayout.WEST, which often causes problems. The problem is, the border layout allocates an initial width to the WEST and EAST regions based on the contained component’s preferred width. For NORTH and SOUTH, it uses the preferred height.

ScrollPane in West

When you first expand a tree node, the BorderLayout does not adjust its width. Thus, you see a horizontal scrollbar as shown in the middle image above.

Even worse, when you resize the window even a tiny bit, the scroll pane instantly skips to a wider size and the scrollbar vanishes.

Some components work reasonably well inside scroll panes in NORTH, SOUTH, EAST, or WEST, but more often than not you will encounter problems. As your layouts become more complex, the results may seem increasingly unpredictable. I recommend you avoid scroll panes in these regions.

Summary

That’s it for the JScrollPane Blunders series of posts. Just like everything else we programmers do, you’ll probably find some rare cases where you can bend or break these rules. Someone asked this in a comment:

Is there no way in Java to get the effect seen in Mac OS X Finder using the View by Column? It is a series of grids, each representing a deeper level in a hierarchy. Each grid is scrollable, and the group is scrollable when the number of grids is too great to fit on screen.

I think the key here is that inner panels have vertical scroll bars, while the outer panel provides the horizontal scroll bar. Thus, you do not simply nest scroll panes inside each other. The inner scroll panes need a vertical only policy, while the outer scroll pane needs horizontal only. Thus you don’t have inner and outer scroll panes competing for scrolling in the same direction.

Something like this:

Finder

That’s about 15 minutes of hacking, and it still has problems. Most notably, the third list (Cities) seems to be truncated by exactly the width of a vertical scrollbar. I’m sure this can be solved; I’ll leave that as an “exercise for the reader”.

And since I do not own a Mac, I don’t really know how the thing is supposed to work anyway. You might want to check out Bug’s work for a more complete and accurate Java implementation.


Bug Says:

Argh now you’ve motivated me to finish the damn example! I do have a file browser like the Mac’s working which is no good for a blog example, however if anyone wants it, please drop me a mail.

Nicholas Daley Says:

Quaqua (http://quaqua.dev.java.net/) includes a component called JBrowser that mimics the Mac OS X Finder column view.