Home / Java Patterns and Pitfalls     frequal.com

Expand and Collapse JSplitPane with One Touch

JSplitPane is a great way to allow users to customize your Swing GUI. It lets users choose how much space is allocated to two adjacent components. It is also a great way to show off how layout managers give Swing GUIs amazing flexibility, handling small spaces and large spaces alike, giving more room to components that need it and keeping compact components small.

However, there is one aspect of JSplitPane's default behavior which is lacking, yet easily addressed with a minor code change. By adding this line:

for each JSplitPane you create, you'll add two arrows to the splitter. The arrows let the user collapse the splitter completely in either direction. Without doing this the splitter will only collapse as small as the minimum size of the component being shrunk, which sometimes leaves the user wanting more.

After this quick change, either side of the splitter can be completely hidden. Then if the users want to see part of the splitter full-size, they can. Restoring the splitter to its original location is easy too, just use the arrow in the opposite direction.

This feature seems very valuable, I think it should be made the default behavior in a future version of Swing.

Last modified on 30 Apr 2008 by AO

Copyright © 2020 Andrew Oliver