javax.swing
Class JScrollPane

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--javax.swing.JComponent
                    |
                    +--javax.swing.JScrollPane
All Implemented Interfaces:
Accessible, ImageObserver, MenuContainer, ScrollPaneConstants, Serializable
public class JScrollPane
extends JComponent
implements ScrollPaneConstants, Accessible

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.

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.

By default, the corners are empty. You can put a component into a corner using setCorner, in case you there is some function or decoration you would like to add to the scroll pane. The size of corner components is entirely determined by the size of the headers and scroll bars that surround them.

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.)

For the keyboard keys used by this component in the standard Look and Feel (L&F) renditions, see the JScrollPane key assignments.

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: 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 JavaBeansTM has been added to the java.beans package. Please see XMLEncoder.

See Also:
JScrollBar, JViewport, ScrollPaneLayout, Scrollable, Component.getPreferredSize(), setViewportView(java.awt.Component), setRowHeaderView(java.awt.Component), setColumnHeaderView(java.awt.Component), setCorner(java.lang.String, java.awt.Component), setViewportBorder(javax.swing.border.Border)

 

Nested Class Summary

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.
 
Nested classes inherited from class javax.swing.JComponent
 
Nested classes inherited from class java.awt.Container
 
Nested classes inherited from class java.awt.Component
Component.BltBufferStrategy, Component.FlipBufferStrategy
 

 

Field Summary

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.
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.
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface javax.swing.ScrollPaneConstants
COLUMN_HEADER, HORIZONTAL_SCROLLBAR, HORIZONTAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_AS_