Java Tips Weblog

  • Blog Stats

    • 2,065,823 hits
  • Categories

  • Archives

  • Advertisements

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

Get The Code

Related Reading

How to Use Split Panes


4 Responses to “Split Pane Synchronizer”

  1. Jack said

    Cool stuff !!!

  2. Rob Camick said

    Thanks for the feedback.

  3. CH said

    Really good and easy to use

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: