APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing

public Class JScrollPane

extends JComponent
implements ScrollPaneConstants, Accessible
Class Inheritance
All Implemented Interfaces
javax.accessibility.Accessible, javax.swing.ScrollPaneConstants
Known Direct Subclasses
sun.awt.X11.XTextAreaPeer.AWTTextPane
Annotations
@JavaBean
defaultProperty:UI
description:A specialized container that manages a viewport, optional scrollbars and headers
@SwingContainer
delegate:getViewport
@SuppressWarnings:serial
Imports
java.awt.Component, .ComponentOrientation, .Insets, .LayoutManager, .Point, .Rectangle, java.beans.BeanProperty, .JavaBean, .PropertyChangeEvent, .PropertyChangeListener, .Transient, java.io.IOException, .ObjectOutputStream, .Serial, javax.accessibility.Accessible, .AccessibleContext, .AccessibleRelation, .AccessibleRole, javax.swing.border.Border, javax.swing.event.ChangeEvent, .ChangeListener, javax.swing.plaf.ScrollPaneUI, .UIResource

Provides a scrollable view of a lightweight component. A JScrollPane manages a viewport, optional vertical and horizontal scroll bars, and optional row and column heading viewports. You can find task-oriented documentation of JScrollPane in How to Use Scroll Panes, a section in The Java Tutorial. Note that JScrollPane does not support heavyweight components.

Example:

The following text describes this image.

The JViewport provides a window, or "viewport" onto a data source -- for example, a text file. That data source is the "scrollable client" (aka data model) displayed by the JViewport view. A JScrollPane basically consists of JScrollBars, a JViewport, and the wiring between them, as shown in the diagram at right.

In addition to the scroll bars and viewport, a JScrollPane can have a column header and a row header. Each of these is a JViewport object that you specify with setRowHeaderView, and setColumnHeaderView. The column header viewport automatically scrolls left and right, tracking the left-right scrolling of the main viewport. (It never scrolls vertically, however.) The row header acts in a similar fashion.

Where two scroll bars meet, the row header meets the column header, or a scroll bar meets one of the headers, both components stop short of the corner, leaving a rectangular space which is, by default, empty. These spaces can potentially exist in any number of the four corners. In the previous diagram, the top right space is present and identified by the label "corner component".

Any number of these empty spaces can be replaced by using the setCorner method to add a component to a particular corner. (Note: The same component cannot be added to multiple corners.) This is useful if there's some extra decoration or function you'd like to add to the scroll pane. The size of each corner component is entirely determined by the size of the headers and/or scroll bars that surround it.

A corner component will only be visible if there is an empty space in that corner for it to exist in. For example, consider a component set into the top right corner of a scroll pane with a column header. If the scroll pane's vertical scrollbar is not present, perhaps because the view component hasn't grown large enough to require it, then the corner component will not be shown (since there is no empty space in that corner created by the meeting of the header and vertical scroll bar). Forcing the scroll bar to always be shown, using setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS), will ensure that the space for the corner component always exists.

To add a border around the main viewport, you can use setViewportBorder. (Of course, you can also add a border around the whole scroll pane using setBorder.)

A common operation to want to do is to set the background color that will be used if the main viewport view is smaller than the viewport, or is not opaque. This can be accomplished by setting the background color of the viewport, via scrollPane.getViewport().setBackground(). The reason for setting the color of the viewport and not the scrollpane is that by default JViewport is opaque which, among other things, means it will completely fill in its background using its background color. Therefore when JScrollPane draws its background the viewport will usually draw over it.

By default JScrollPane uses ScrollPaneLayout to handle the layout of its child Components. ScrollPaneLayout determines the size to make the viewport view in one of two ways:

  1. If the view implements Scrollable a combination of getPreferredScrollableViewportSize, getScrollableTracksViewportWidth and getScrollableTracksViewportHeightis used, otherwise
  2. getPreferredSize is used.

Warning

Swing is not thread safe. For more information see Swing'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 the java.beans package. Please see java.beans.XMLEncoder.

Author
Hans Muller
Since
1.2
See Also
JScrollBar, JViewport, ScrollPaneLayout, Scrollable, Component#getPreferredSize, setViewportView, setRowHeaderView, setColumnHeaderView, setCorner, setViewportBorder

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
JScrollPane.AccessibleJScrollPane

This class implements accessibility support for the JScrollPane class.

protected class
JScrollPane.ScrollBar

By default JScrollPane creates scrollbars that are instances of this class.

Field Summary

Modifier and TypeField and Description
protected JViewport
columnHeader

The column header child.

protected JScrollBar
horizontalScrollBar

The scrollpane's horizontal scrollbar child.

protected int
horizontalScrollBarPolicy

The display policy for the horizontal scrollbar.

protected Component
lowerLeft

The component to display in the lower left corner.

protected Component
lowerRight

The component to display in the lower right corner.

protected JViewport
rowHeader

The row header child.

private static final String
protected Component
upperLeft

The component to display in the upper left corner.

protected Component
upperRight

The component to display in the upper right corner.

protected JScrollBar
verticalScrollBar

The scrollpane's vertical scrollbar child.

protected int
verticalScrollBarPolicy

The display policy for the vertical scrollbar.

protected JViewport
viewport

The scrollpane's viewport child.

private Border
private boolean
Inherited from javax.swing.JComponent:
DEBUG_GRAPHICS_LOADEDfocusControllerivSourceComponentlistenerListpaintingChildTOOL_TIP_TEXT_KEYuiUNDEFINED_CONDITIONWHEN_ANCESTOR_OF_FOCUSED_COMPONENTWHEN_FOCUSEDWHEN_IN_FOCUSED_WINDOW

Constructor Summary

AccessConstructor and Description
public
JScrollPane(Component
the component to display in the scrollpanes viewport
view
,
int
an integer that specifies the vertical scrollbar policy
vsbPolicy
,
int
an integer that specifies the horizontal scrollbar policy
hsbPolicy
)

Creates a JScrollPane that displays the view component in a viewport whose view position can be controlled with a pair of scrollbars.

public
JScrollPane(Component
the component to display in the scrollpane's viewport
view
)

Creates a JScrollPane that displays the contents of the specified component, where both horizontal and vertical scrollbars appear whenever the component's contents are larger than the view.

public
JScrollPane(int
an integer that specifies the vertical scrollbar policy
vsbPolicy
,
int
an integer that specifies the horizontal scrollbar policy
hsbPolicy
)

Creates an empty (no viewport view) JScrollPane with specified scrollbar policies.

public
JScrollPane()

Creates an empty (no viewport view) JScrollPane where both horizontal and vertical scrollbars appear when needed.

Method Summary

Modifier and TypeMethod and Description
public JScrollBar

Returns:

a JScrollBar with a horizontal orientation
createHorizontalScrollBar
()

Returns a JScrollPane.ScrollBar by default.

public JScrollBar

Returns:

a JScrollBar with a vertical orientation
createVerticalScrollBar
()

Returns a JScrollPane.ScrollBar by default.

protected JViewport

Returns:

a new JViewport
createViewport
()

Returns a new JViewport by default.

public AccessibleContext

Returns:

an AccessibleJScrollPane that serves as the AccessibleContext of this JScrollPane
getAccessibleContext
()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this JScrollPane.

public JViewport

Returns:

the columnHeader property
getColumnHeader
()

Returns the column header.

public Component

Returns:

the corner component (which may be null) identified by the given key, or null if the key is invalid
getCorner
(String
one of the values as shown above
key
)

Returns the component at the specified corner.

public JScrollBar

Returns:

the horizontalScrollBar property
getHorizontalScrollBar
()

Returns the horizontal scroll bar that controls the viewport's horizontal view position.

public int

Returns:

the horizontalScrollBarPolicy property
getHorizontalScrollBarPolicy
()

Returns the horizontal scroll bar policy value.

public JViewport

Returns:

the rowHeader property
getRowHeader
()

Returns the row header.

public ScrollPaneUI

Returns:

the ScrollPaneUI object that renders this component
getUI
()

Overrides javax.swing.JComponent.getUI.

Returns the look and feel (L&F) object that renders this component.

public String

Returns:

the string "ScrollPaneUI"
getUIClassID
()

Overrides javax.swing.JComponent.getUIClassID.

Returns the suffix used to construct the name of the L&F class used to render this component.

public JScrollBar

Returns:

the verticalScrollBar property
getVerticalScrollBar
()

Returns the vertical scroll bar that controls the viewports vertical view position.

public int

Returns:

the verticalScrollBarPolicy property
getVerticalScrollBarPolicy
()

Returns the vertical scroll bar policy value.

public JViewport

Returns:

the viewport property
getViewport
()

Returns the current JViewport.

public Border

Returns:

the viewportBorder property
getViewportBorder
()

Returns the Border object that surrounds the viewport.

public Rectangle

Returns:

a Rectangle object specifying the viewport border
getViewportBorderBounds
()

Returns the bounds of the viewport's border.

public boolean

Returns:

true
isValidateRoot
()

Overrides javax.swing.JComponent.isValidateRoot.

Overridden to return true so that any calls to revalidate on any descendants of this JScrollPane will cause the entire tree beginning with this JScrollPane to be validated.

public boolean

Returns:

true if mouse wheel scrolling is enabled, false otherwise
isWheelScrollingEnabled
()

Indicates whether or not scrolling will take place in response to the mouse wheel.

protected String

Returns:

a string representation of this JScrollPane.
paramString
()

Overrides javax.swing.JComponent.paramString.

Returns a string representation of this JScrollPane.

public void
setColumnHeader(JViewport
a JViewport which is the new column header
columnHeader
)

Removes the old columnHeader, if it exists; if the new columnHeader isn't null, syncs the x coordinate of its viewPosition with the viewport (if there is one) and then adds it to the scroll pane.

public void
setColumnHeaderView(Component
the component to display as the column header
view
)

Creates a column-header viewport if necessary, sets its view, and then adds the column-header viewport to the scrollpane.

public void
setComponentOrientation(ComponentOrientation
one of the following values:
  • java.awt.ComponentOrientation.LEFT_TO_RIGHT
  • java.awt.ComponentOrientation.RIGHT_TO_LEFT
  • java.awt.ComponentOrientation.UNKNOWN
co
)

Overrides java.awt.Component.setComponentOrientation.

Sets the orientation for the vertical and horizontal scrollbars as determined by the ComponentOrientation argument.

public void
setCorner(String
identifies which corner the component will appear in
key
,
Component
one of the following components:
  • lowerLeft
  • lowerRight
  • upperLeft
  • upperRight
corner
)

Adds a child that will appear in one of the scroll panes corners, if there's room.

public void
setHorizontalScrollBar(JScrollBar
the horizontal scrollbar to be added
horizontalScrollBar
)

Adds the scrollbar that controls the viewport's horizontal view position to the scrollpane.

public void
setHorizontalScrollBarPolicy(int
one of the three values listed above
policy
)

Determines when the horizontal scrollbar appears in the scrollpane.

public void
setLayout(LayoutManager
the specified layout manager
layout
)

Overrides java.awt.Container.setLayout.

Sets the layout manager for this JScrollPane.

public void
setRowHeader(JViewport
the new row header to be used; if null the old row header is still removed and the new rowHeader is set to null
rowHeader
)

Removes the old rowHeader, if it exists; if the new rowHeader isn't null, syncs the y coordinate of its viewPosition with the viewport (if there is one) and then adds it to the scroll pane.

public void
setRowHeaderView(Component
the component to display as the row header
view
)

Creates a row-header viewport if necessary, sets its view and then adds the row-header viewport to the scrollpane.

public void
setUI(ScrollPaneUI
the ScrollPaneUI L&F object
ui
)

Sets the ScrollPaneUI object that provides the look and feel (L&F) for this component.

public void
setVerticalScrollBar(JScrollBar
the new vertical scrollbar to be added
verticalScrollBar
)

Adds the scrollbar that controls the viewports vertical view position to the scrollpane.

public void
setVerticalScrollBarPolicy(int
one of the three values listed above
policy
)

Determines when the vertical scrollbar appears in the scrollpane.

public void
setViewport(JViewport
the new viewport to be used; if viewport is null, the old viewport is still removed and the new viewport is set to null
viewport
)

Removes the old viewport (if there is one); forces the viewPosition of the new viewport to be in the +x,+y quadrant; syncs up the row and column headers (if there are any) with the new viewport; and finally syncs the scrollbars and headers with the new viewport.

public void
setViewportBorder(Border
the border to be added
viewportBorder
)

Adds a border around the viewport.

public void
setViewportView(Component
the component to add to the viewport
view
)

Creates a viewport if necessary and then sets its view.

public void
setWheelScrollingEnabled(boolean
true if scrolling should be done automatically for a MouseWheelEvent, false otherwise.
handleWheel
)

Enables/disables scrolling in response to movement of the mouse wheel.

public void
updateUI()

Overrides javax.swing.JComponent.updateUI.

Replaces the current ScrollPaneUI object with a version from the current default look and feel.

private void
writeObject(ObjectOutputStream s)

Hides javax.swing.JComponent.writeObject.

See readObject and writeObject in JComponent for more information about serialization in Swing.

Inherited from javax.swing.JComponent:
_paintImmediatelyaddAncestorListeneraddNotifyaddVetoableChangeListeneralwaysOnTopcheckIfChildObscuredBySiblingclientPropertyChangedcomponentInputMapChangedcomputeVisibleRectcomputeVisibleRectcompWriteObjectNotifycontainscreateToolTipdisabledndDonedropLocationForPointenablefirePropertyChangefirePropertyChangefirePropertyChangefireVetoableChangegetActionForKeyStrokegetActionMapgetActionMapgetAlignmentXgetAlignmentYgetAncestorListenersgetAutoscrollsgetBaselinegetBaselineResizeBehaviorgetBordergetBoundsgetClientPropertygetComponentGraphicsgetComponentPopupMenugetConditionForKeyStrokegetCreatedDoubleBuffergetDebugGraphicsOptionsgetDefaultLocalegetFontMetricsgetGraphicsgetGraphicsInvokedgetHeightgetInheritsPopupMenugetInputMapgetInputMapgetInputMapgetInputVerifiergetInsetsgetInsetsgetListenersgetLocationgetManagingFocusBackwardTraversalKeysgetManagingFocusForwardTraversalKeysgetMaximumSizegetMinimumSizegetNextFocusableComponentgetPopupLocationgetPreferredSizegetRegisteredKeyStrokesgetRootPanegetSizegetToolTipLocationgetToolTipTextgetToolTipTextgetTopLevelAncestorgetTransferHandlergetVerifyInputWhenFocusTargetgetVetoableChangeListenersgetVisibleRectgetWidthgetWriteObjCountergetXgetYgrabFocushideisDoubleBufferedisLightweightComponentisManagingFocusisOpaqueisOptimizedDrawingEnabledisPaintingisPaintingForPrintisPaintingOriginisPaintingTileisRequestFocusEnabledpaintpaintBorderpaintChildrenpaintComponentpaintForceDoubleBufferedpaintImmediatelypaintImmediatelypaintToOffscreenprintprintAllprintBorderprintChildrenprintComponentprocessComponentKeyEventprocessKeyBindingprocessKeyBindingsprocessKeyBindingsForAllComponentsprocessKeyEventprocessMouseEventprocessMouseMotionEventputClientPropertyrectangleIsObscuredregisterKeyboardActionregisterKeyboardActionremoveAncestorListenerremoveNotifyremoveVetoableChangeListenerrepaintrepaintrequestDefaultFocusrequestFocusrequestFocusrequestFocusInWindowrequestFocusInWindowresetKeyboardActionsreshaperevalidatesafelyGetGraphicssafelyGetGraphicsscrollRectToVisiblesetActionMapsetAlignmentXsetAlignmentYsetAutoscrollssetBackgroundsetBordersetComponentPopupMenusetCreatedDoubleBuffersetDebugGraphicsOptionssetDefaultLocalesetDoubleBufferedsetDropLocationsetEnabledsetFocusTraversalKeyssetFontsetForegroundsetInheritsPopupMenusetInputMapsetInputVerifiersetMaximumSizesetMinimumSizesetNextFocusableComponentsetOpaquesetPaintingChildsetPreferredSizesetRequestFocusEnabledsetToolTipTextsetTransferHandlersetUIsetUIPropertysetVerifyInputWhenFocusTargetsetVisiblesetWriteObjCountershouldDebugGraphicssuperProcessMouseMotionEventunregisterKeyboardActionupdate