Contents

Swinging Duke
Feedback Button
Left ArrowRight Arrow

 

The Swing Splitter Control

Swing provides a user-interface component named JSplitPane that can be used for partitioning components. With the JSplitPane component, you can manipulate the positioning of other components using a divider-style splitter bar. Because this divider makes use of Swing's look-and-feel-specific (L&F-specific) capabilities, you can use the JSplitPane class to define individual looks and behaviors for different kinds of splitter bars.


Features of the JSplitPane Class

The JSplitPane class provides constructors for dividing two components. Thus, you can use a single JSplitPane object to divide either one or two components.

You can use a JSplitPane object to align components either from left to right (JSplitPane.HORIZONTAL_SPLIT)or from top to bottom (JSplitPane.VERTICAL_SPLIT). The look and feel that you use not only defines the appearance of the divider, but also specifies how the user can resize components with the divider's splitter bar. You can configure a JSplitPane object in such a way that it continually provides information about components that are being resized. Alternatively, you can make JSplitPane's resizing information available only when the user releases the divider.


The JSplitPane API

JSplitPane is a subclass of JComponent. It is defined by the following methods:

public class JSplitPane extends JComponent
{
  public JSplitPane(int orientation);
  public JSplitPane(int orientation, boolean continuousLayout);
  public JSplitPane(int orientation, Component firstComponent,;
                    Component secondComponent);
  public JSplitPane(int orientation, boolean continuousLayout,;
                    Component firstComponent, Component secondComponent);

  public void setOrientation(int);
  public int getOrientation();

  public void setContinuousLayout(boolean);
  public boolean getContinuousLayout();

  public void setLeftComponent(Component component);
  public Component getLeftComponent();
  public void setTopComponent(Component component);
  public Component getTopComponent();
  public void setRightComponent(Component component);
  public Component getRightComponent();
  public void setBottomComponent(Component component);
  public Component getBottomComponent();
  public void resetToPreferredSizes();

  public void setDividerLocation(int location);
  public int getDividerLocation();
}

The JSplitPane works by using a custom layout manager. This layout manager is responsible for appropriately positioning resizable components based on their orientation. To alter the location of a JSplitPanel divider, you can message setDividerLocation. Alternatively, you can leave it up to the JSplitPane layout manager to detect when the size of one resizable component has been altered, and to adjust the size of the other component accordingly. When you call the resetToPreferredSizes() method, the JSplitPane layout manager relays out the preferred sizes of the components that are being resized.


BasicSplitPaneUI

A class named BasicSplitPaneUI provides the basic look and feel of the JSplitPane class. By default, a JSplitPane divider is an instance of BasicSplitPaneDivider, which in turn is a subclass of Container. Because BasicSplitPaneUI subclasses from Container, you can create a customized divider by adding components to BasicSplitPaneUI. If you take advantage of this capability of BasicSplitPaneUI, just be sure to message JSplitPane with resetToPreferredSize() if you need to change the size of your divider.


BasicSplitPaneDivider

You can use BasicSplitPaneDivider to create a border for a JSplitPane divider, as well as to set the preferred sizes of divided components. Also, because BasicSplitPaneDivider descends from Container, you can add other components to it. If you want to create a custom divider, it is recommended that you subclass BasicSplitPaneDivider, which can handle resizing for you. BasicSplitPaneDivider defines the following public methods:

public class BasicSplitPaneDivider extends Container
{
  public BasicSplitPaneDivider(BasicSplitPaneUI ui);
  public void setDividerSize(int newSize);
  public int getDividerSize();
  public void setBorder(Border border);
  public Border getBorder();
}

Arrows


Version 0.5. Last modified 10/1/97.
Copyright © 1995-97 Sun Microsystems, Inc. All Rights Reserved.

Sun's Home Page