Class JSplitPane

java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JSplitPane
All Implemented Interfaces:
ImageObserver,MenuContainer,Serializable,Accessible

@JavaBean(defaultProperty="UI")public classJSplitPaneextendsJComponentimplementsAccessible
JSplitPane is used to divide two (and only two)Components. The twoComponents are graphically divided based on the look and feel implementation, and the twoComponents can then be interactively resized by the user. Information on usingJSplitPane is inHow to Use Split Panes inThe Java Tutorial.

The twoComponents in a split pane can be aligned left to right usingJSplitPane.HORIZONTAL_SPLIT, or top to bottom usingJSplitPane.VERTICAL_SPLIT. The preferred way to change the size of theComponents is to invokesetDividerLocation wherelocation is either the new x or y position, depending on the orientation of theJSplitPane.

To resize theComponents to their preferred sizes invokeresetToPreferredSizes.

When the user is resizing theComponents the minimum size of theComponents is used to determine the maximum/minimum position theComponents can be set to. If the minimum size of the two components is greater than the size of the split pane the divider will not allow you to resize it. To alter the minimum size of aJComponent, seeJComponent.setMinimumSize(java.awt.Dimension).

When the user resizes the split pane the new space is distributed between the two components based on theresizeWeight property. A value of 0, the default, indicates the right/bottom component gets all the space, where as a value of 1 indicates the left/top component gets all the space.

Warning: Swing is not thread safe. For more information seeSwing's Threading Policy.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans has been added to thejava.beans package. Please seeXMLEncoder.

Since:
1.2
See Also:
  • Field Details

    • VERTICAL_SPLIT

      public static final int VERTICAL_SPLIT
      Vertical split indicates theComponents are split along the y axis. For example the twoComponents will be split one on top of the other.
      See Also:
    • HORIZONTAL_SPLIT

      public static final int HORIZONTAL_SPLIT
      Horizontal split indicates theComponents are split along the x axis. For example the twoComponents will be split one to the left of the other.
      See Also:
    • LEFT

      public static final String LEFT
      Used to add aComponent to the left of the otherComponent.
      See Also:
    • RIGHT

      public static final String RIGHT
      Used to add aComponent to the right of the otherComponent.
      See Also:
    • TOP

      public static final String TOP
      Used to add aComponent above the otherComponent.
      See Also:
    • BOTTOM

      public static final String BOTTOM
      Used to add aComponent below the otherComponent.
      See Also:
    • DIVIDER

      public static final String DIVIDER
      Used to add aComponent that will represent the divider.
      See Also:
    • ORIENTATION_PROPERTY

      public static final String ORIENTATION_PROPERTY
      Bound property name for orientation (horizontal or vertical).
      See Also:
    • CONTINUOUS_LAYOUT_PROPERTY

      public static final String CONTINUOUS_LAYOUT_PROPERTY
      Bound property name for continuousLayout.
      See Also:
    • DIVIDER_SIZE_PROPERTY

      public static final String DIVIDER_SIZE_PROPERTY
      Bound property name for border.
      See Also:
    • ONE_TOUCH_EXPANDABLE_PROPERTY

      public static final String ONE_TOUCH_EXPANDABLE_PROPERTY
      Bound property for oneTouchExpandable.
      See Also:
    • LAST_DIVIDER_LOCATION_PROPERTY

      public static final String LAST_DIVIDER_LOCATION_PROPERTY
      Bound property for lastLocation.
      See Also:
    • DIVIDER_LOCATION_PROPERTY

      public static final String DIVIDER_LOCATION_PROPERTY
      Bound property for the dividerLocation.
      Since:
      1.3
      See Also:
    • RESIZE_WEIGHT_PROPERTY

      public static final String RESIZE_WEIGHT_PROPERTY
      Bound property for weight.
      Since:
      1.3
      See Also:
    • orientation

      protected int orientation
      How the views are split.
    • continuousLayout

      protected boolean continuousLayout
      Whether or not the views are continuously redisplayed while resizing.
    • leftComponent

      protected Component leftComponent
      The left or top component.
    • rightComponent

      protected Component rightComponent
      The right or bottom component.
    • dividerSize

      protected int dividerSize
      Size of the divider.
    • oneTouchExpandable

      protected boolean oneTouchExpandable
      Is a little widget provided to quickly expand/collapse the split pane?
    • lastDividerLocation

      protected int lastDividerLocation
      Previous location of the split pane.
  • Constructor Details

    • JSplitPane

      public JSplitPane()
      Creates a newJSplitPane configured to arrange the child components side-by-side horizontally, using two buttons for the components.
    • JSplitPane

      @ConstructorProperties("orientation")public JSplitPane(int newOrientation)
      Creates a newJSplitPane configured with the specified orientation.
      Parameters:
      newOrientation -JSplitPane.HORIZONTAL_SPLIT orJSplitPane.VERTICAL_SPLIT
      Throws:
      IllegalArgumentException - iforientation is not one of HORIZONTAL_SPLIT or VERTICAL_SPLIT.
    • JSplitPane

      public JSplitPane(int newOrientation, boolean newContinuousLayout)
      Creates a newJSplitPane with the specified orientation and redrawing style.
      Parameters:
      newOrientation -JSplitPane.HORIZONTAL_SPLIT orJSplitPane.VERTICAL_SPLIT
      newContinuousLayout - a boolean, true for the components to redraw continuously as the divider changes position, false to wait until the divider position stops changing to redraw
      Throws:
      IllegalArgumentException - iforientation is not one of HORIZONTAL_SPLIT or VERTICAL_SPLIT
    • JSplitPane

      public JSplitPane(int newOrientation,Component newLeftComponent,Component newRightComponent)
      Creates a newJSplitPane with the specified orientation and the specified components.
      Parameters:
      newOrientation -JSplitPane.HORIZONTAL_SPLIT orJSplitPane.VERTICAL_SPLIT
      newLeftComponent - theComponent that will appear on the left of a horizontally-split pane, or at the top of a vertically-split pane
      newRightComponent - theComponent that will appear on the right of a horizontally-split pane, or at the bottom of a vertically-split pane
      Throws:
      IllegalArgumentException - iforientation is not one of: HORIZONTAL_SPLIT or VERTICAL_SPLIT
    • JSplitPane

      public JSplitPane(int newOrientation, boolean newContinuousLayout,Component newLeftComponent,Component newRightComponent)
      Creates a newJSplitPane with the specified orientation and redrawing style, and with the specified components.
      Parameters:
      newOrientation -JSplitPane.HORIZONTAL_SPLIT orJSplitPane.VERTICAL_SPLIT
      newContinuousLayout - a boolean, true for the components to redraw continuously as the divider changes position, false to wait until the divider position stops changing to redraw
      newLeftComponent - theComponent that will appear on the left of a horizontally-split pane, or at the top of a vertically-split pane
      newRightComponent - theComponent that will appear on the right of a horizontally-split pane, or at the bottom of a vertically-split pane
      Throws:
      IllegalArgumentException - iforientation is not one of HORIZONTAL_SPLIT or VERTICAL_SPLIT
  • Method Details

    • setComponentOrientation

      public void setComponentOrientation(ComponentOrientation orientation)
      Sets the language-sensitive orientation that is to be used to order the elements or text within this component. Language-sensitiveLayoutManager andComponent subclasses will use this property to determine how to lay out and draw components.

      At construction time, a component's orientation is set toComponentOrientation.UNKNOWN, indicating that it has not been specified explicitly. The UNKNOWN orientation behaves the same asComponentOrientation.LEFT_TO_RIGHT.

      To set the orientation of a single component, use this method. To set the orientation of an entire component hierarchy, useapplyComponentOrientation.

      This method changes layout-related information, and therefore, invalidates the component hierarchy.

      Overrides:
      setComponentOrientation in class Component
      Parameters:
      orientation - the orientation to be set
      See Also:
    • setEnabled

      public void setEnabled(boolean enabled)
      Sets whether or not this component is enabled. A component that is enabled may respond to user input, while a component that is not enabled cannot respond to user input. Some components may alter their visual representation when they are disabled in order to provide feedback to the user that they cannot take input.

      Note: Disabling a component does not disable its children.

      Note: Disabling a lightweight component does not prevent it from receiving MouseEvents.

      Overrides:
      setEnabled in class JComponent
      Parameters:
      enabled - true if this component should be enabled, false otherwise
      See Also:
    • setUI

      public void setUI(SplitPaneUI ui)
      Sets the L&F object that renders this component.
      Parameters:
      ui - theSplitPaneUI L&F object
      See Also:
    • getUI

      @BeanProperty(bound=false,expert=true,description="The L&F object that renders this component.")public SplitPaneUI getUI()
      Returns theSplitPaneUI that is providing the current look and feel.
      Overrides:
      getUI in class JComponent
      Returns:
      theSplitPaneUI object that renders this component
    • updateUI

      public void updateUI()
      Notification from theUIManager that the L&F has changed. Replaces the current UI object with the latest version from theUIManager.
      Overrides:
      updateUI in class JComponent
      See Also:
    • getUIClassID

      @BeanProperty(bound=false,expert=true,description="A string that specifies the name of the L&F class.")public String getUIClassID()
      Returns the name of the L&F class that renders this component.
      Overrides:
      getUIClassID in class JComponent
      Returns:
      the string "SplitPaneUI"
      See Also:
    • setDividerSize

      @BeanProperty(description="The size of the divider.")public void setDividerSize(int newSize)
      Sets the size of the divider. Divider sizesnewSize < 0 are ignored.
      Parameters:
      newSize - an integer giving the size of the divider in pixels
    • getDividerSize

      public int getDividerSize()
      Returns the size of the divider.
      Returns:
      an integer giving the size of the divider in pixels
    • setLeftComponent

      public void setLeftComponent(Component comp)
      Sets the component to the left (or above) the divider.
      Parameters:
      comp - theComponent to display in that position
    • getLeftComponent

      @BeanProperty(bound=false,preferred=true,description="The component to the left (or above) the divider.")public Component getLeftComponent()
      Returns the component to the left (or above) the divider.
      Returns:
      theComponent displayed in that position
    • setTopComponent

      @BeanProperty(bound=false,description="The component above, or to the left of the divider.")public void setTopComponent(Component comp)
      Sets the component above, or to the left of the divider.
      Parameters:
      comp - theComponent to display in that position
    • getTopComponent

      public Component getTopComponent()
      Returns the component above, or to the left of the divider.
      Returns:
      theComponent displayed in that position
    • setRightComponent

      @BeanProperty(bound=false,preferred=true,description="The component to the right (or below) the divider.")public void setRightComponent(Component comp)
      Sets the component to the right (or below) the divider.
      Parameters:
      comp - theComponent to display in that position
    • getRightComponent

      public Component getRightComponent()
      Returns the component to the right (or below) the divider.
      Returns:
      theComponent displayed in that position
    • setBottomComponent

      @BeanProperty(bound=false,description="The component below, or to the right of the divider.")public void setBottomComponent(Component comp)
      Sets the component below, or to the right of the divider.
      Parameters:
      comp - theComponent to display in that position
    • getBottomComponent

      public Component getBottomComponent()
      Returns the component below, or to the right of the divider.
      Returns:
      theComponent displayed in that position
    • setOneTouchExpandable

      @BeanProperty(description="UI widget on the divider to quickly expand/collapse the divider.")public void setOneTouchExpandable(boolean newValue)
      Sets the value of theoneTouchExpandable property, which must betrue for theJSplitPane to provide a UI widget on the divider to quickly expand/collapse the divider. The default value of this property isfalse. Some look and feels might not support one-touch expanding; they will ignore this property.
      Parameters:
      newValue -true to specify that the split pane should provide a collapse/expand widget
      See Also:
    • isOneTouchExpandable

      public boolean isOneTouchExpandable()
      Gets theoneTouchExpandable property.
      Returns:
      the value of theoneTouchExpandable property
      See Also:
    • setLastDividerLocation

      @BeanProperty(description="The last location the divider was at.")public void setLastDividerLocation(int newLastLocation)
      Sets the last location the divider was at tonewLastLocation.
      Parameters:
      newLastLocation - an integer specifying the last divider location in pixels, from the left (or upper) edge of the pane to the left (or upper) edge of the divider
    • getLastDividerLocation

      public int getLastDividerLocation()
      Returns the last location the divider was at.
      Returns:
      an integer specifying the last divider location as a count of pixels from the left (or upper) edge of the pane to the left (or upper) edge of the divider
    • setOrientation

      @BeanProperty(enumerationValues={"JSplitPane.HORIZONTAL_SPLIT","JSplitPane.VERTICAL_SPLIT"},description="The orientation, or how the splitter is divided.")public void setOrientation(int orientation)
      Sets the orientation, or how the splitter is divided. The options are:
      • JSplitPane.VERTICAL_SPLIT (above/below orientation of components)
      • JSplitPane.HORIZONTAL_SPLIT (left/right orientation of components)
      Parameters:
      orientation - an integer specifying the orientation
      Throws:
      IllegalArgumentException - if orientation is not one of: HORIZONTAL_SPLIT or VERTICAL_SPLIT.
    • getOrientation

      public int getOrientation()
      Returns the orientation.
      Returns:
      an integer giving the orientation
      See Also:
    • setContinuousLayout

      @BeanProperty(description="Whether the child components are continuously redisplayed and laid out during user intervention.")public void setContinuousLayout(boolean newContinuousLayout)
      Sets the value of thecontinuousLayout property, which must betrue for the child components to be continuously redisplayed and laid out during user intervention. The default value of this property is look and feel dependent. Some look and feels might not support continuous layout; they will ignore this property.
      Parameters:
      newContinuousLayout -true if the components should continuously be redrawn as the divider changes position
      See Also:
    • isContinuousLayout

      public boolean isContinuousLayout()
      Gets thecontinuousLayout property.
      Returns:
      the value of thecontinuousLayout property
      See Also:
    • setResizeWeight

      @BeanProperty(description="Specifies how to distribute extra space when the split pane resizes.")public void setResizeWeight(double value)
      Specifies how to distribute extra space when the size of the split pane changes. A value of 0, the default, indicates the right/bottom component gets all the extra space (the left/top component acts fixed), where as a value of 1 specifies the left/top component gets all the extra space (the right/bottom component acts fixed). Specifically, the left/top component gets (weight * diff) extra space and the right/bottom component gets (1 - weight) * diff extra space.
      Parameters:
      value - as described above
      Throws:
      IllegalArgumentException - ifvalue is < 0 or > 1
      Since:
      1.3
    • getResizeWeight

      public double getResizeWeight()
      Returns the number that determines how extra space is distributed.
      Returns:
      how extra space is to be distributed on a resize of the split pane
      Since:
      1.3
    • resetToPreferredSizes

      public void resetToPreferredSizes()
      Lays out theJSplitPane layout based on the preferred size of the children components. This will likely result in changing the divider location.
    • setDividerLocation

      @BeanProperty(description="The location of the divider.")public void setDividerLocation(double proportionalLocation)
      Sets the divider location as a percentage of theJSplitPane's size.

      This method is implemented in terms ofsetDividerLocation(int). This method immediately changes the size of the split pane based on its current size. If the split pane is not correctly realized and on screen, this method will have no effect (new divider location will become (current size * proportionalLocation) which is 0).

      Parameters:
      proportionalLocation - a double-precision floating point value that specifies a percentage, from zero (top/left) to 1.0 (bottom/right)
      Throws:
      IllegalArgumentException - if the specified location is < 0 or > 1.0
    • setDividerLocation

      @BeanProperty(description="The location of the divider.")public void setDividerLocation(int location)
      Sets the location of the divider. This is passed off to the look and feel implementation, and then listeners are notified. A value less than 0 implies the divider should be reset to a value that attempts to honor the preferred size of the left/top component. After notifying the listeners, the last divider location is updated, viasetLastDividerLocation.
      Parameters:
      location - an int specifying a UI-specific value (typically a pixel count)
    • getDividerLocation

      public int getDividerLocation()
      Returns the last value passed tosetDividerLocation. The value returned from this method may differ from the actual divider location (ifsetDividerLocation was passed a value bigger than the current size).
      Returns:
      an integer specifying the location of the divider
    • getMinimumDividerLocation

      @BeanProperty(bound=false,description="The minimum location of the divider from the L&F.")public int getMinimumDividerLocation()
      Returns the minimum location of the divider from the look and feel implementation.
      Returns:
      an integer specifying a UI-specific value for the minimum location (typically a pixel count); or -1 if the UI isnull
    • getMaximumDividerLocation

      @BeanProperty(bound=false)public int getMaximumDividerLocation()
      Returns the maximum location of the divider from the look and feel implementation.
      Returns:
      an integer specifying a UI-specific value for the maximum location (typically a pixel count); or -1 if the UI isnull
    • remove

      public void remove(Component component)
      Removes the child component,component from the pane. Resets theleftComponent orrightComponent instance variable, as necessary.
      Overrides:
      remove in class Container
      Parameters:
      component - theComponent to remove
      See Also:
    • remove

      public void remove(int index)
      Removes theComponent at the specified index. Updates theleftComponent andrightComponent instance variables as necessary, and then messages super.
      Overrides:
      remove in class Container
      Parameters:
      index - an integer specifying the component to remove, where 1 specifies the left/top component and 2 specifies the bottom/right component
      See Also:
    • removeAll

      public void removeAll()
      Removes all the child components from the split pane. Resets theleftComponent andrightComponent instance variables.
      Overrides:
      removeAll in class Container
      See Also:
    • isValidateRoot

      @BeanProperty(hidden=true)public boolean isValidateRoot()
      Returns true, so that calls torevalidate on any descendant of thisJSplitPane will cause a request to be queued that will validate theJSplitPane and all its descendants.
      Overrides:
      isValidateRoot in class JComponent
      Returns:
      true
      See Also:
    • addImpl

      protected void addImpl(Component comp,Object constraints, int index)
      Adds the specified component to this split pane. Ifconstraints identifies the left/top or right/bottom child component, and a component with that identifier was previously added, it will be removed and thencomp will be added in its place. Ifconstraints is not one of the known identifiers the layout manager may throw anIllegalArgumentException.

      The possible constraints objects (Strings) are:

      • JSplitPane.TOP
      • JSplitPane.LEFT
      • JSplitPane.BOTTOM
      • JSplitPane.RIGHT
      If theconstraints object isnull, the component is added in the first available position (left/top if open, else right/bottom).

      Overrides:
      addImpl in class Container
      Parameters:
      comp - the component to add
      constraints - anObject specifying the layout constraints (position) for this component
      index - an integer specifying the index in the container's list.
      Throws:
      IllegalArgumentException - if theconstraints object does not match an existing component
      See Also:
    • paintChildren

      protected void paintChildren(Graphics g)
      Subclassed to message the UI withfinishedPaintingChildren after super has been messaged, as well as painting the border.
      Overrides:
      paintChildren in class JComponent
      Parameters:
      g - theGraphics context within which to paint
      See Also:
    • paramString

      protected String paramString()
      Returns a string representation of thisJSplitPane. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not benull.
      Overrides:
      paramString in class JComponent
      Returns:
      a string representation of thisJSplitPane.
    • getAccessibleContext

      @BeanProperty(bound=false,expert=true,description="The AccessibleContext associated with this SplitPane.")public AccessibleContext getAccessibleContext()
      Gets the AccessibleContext associated with this JSplitPane. For split panes, the AccessibleContext takes the form of an AccessibleJSplitPane. A new AccessibleJSplitPane instance is created if necessary.
      Specified by:
      getAccessibleContext in interface Accessible
      Overrides:
      getAccessibleContext in class Component
      Returns:
      an AccessibleJSplitPane that serves as the AccessibleContext of this JSplitPane