Split Pane Synchronizer
Posted by Rob Camick on March 19, 2009
A JSplitPane is commonly used to display 2 related Components. A divider is painted between the two Components and the user can drag this divider to control the relative space available to each Component. An interesting usage of the split pane might be to nest split panes inside the main split pane.
This would result in a GUI looking something like this:
Note now that two horizontal dividers exist, one for each nested split pane. Additionally, you may have a requirement to keep the divider location synchronized between the two split panes. Normally this would be done by sharing a model betweeen the two components. Unfortunately, a JSplitPane does not have a model to share so we need to find a different approach. Luckily, a split pane does generate PropertyChangeEvents whenever the divider location changes.
The SplitPaneSychronizer is a convenience class that will handle these PropertyChangeEvents and update the divider location of all related split panes. All you need to do is specify the related split panes.
Code to use the SplitPaneSynchronizer in the example GUI from above would be something like:
JSplitPane left = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
left.setTopComponent( redPanel );
left.setBottomComponent( greenPanel );
JSplitPane right = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
right.setTopComponent( bluePanel );
right.setBottomComponent( whitePanel );
JSplitPane main = new JSplitPane();
main.setLeftComponent( left );
main.setRightComponent( right );
new SplitPaneSynchronizer(left, right);
Now the two horizontal dividers will be synchronized. The SplitPaneSynchronizer will work when:
- the divider is dragged
- the one touch expandable feature is used
- the setDividerLocation(…) method is used