|
JSR-209 (Final Release) | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JScrollPane
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.
|
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 JScrollBar
s,
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
.)
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:
Scrollable
a combination of getPreferredScrollableViewportSize
,
getScrollableTracksViewportWidth
and
getScrollableTracksViewportHeight
is used, otherwise
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 java.beans.XMLEncoder
.
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.ScrollBar
By default JScrollPane creates scrollbars
that are instances
of this class. |
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 java.awt.Component |
lowerLeft
The component to display in the lower left corner. |
protected java.awt.Component |
lowerRight
The component to display in the lower right corner. |
protected JViewport |
rowHeader
The row header child. |
protected java.awt.Component |
upperLeft
The component to display in the upper left corner. |
protected java.awt.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 |
listenerList, TOOL_TIP_TEXT_KEY, 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 java.awt.image.ImageObserver |
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH |
Constructor Summary | |
JScrollPane()
Creates an empty (no viewport view) JScrollPane
where both horizontal and vertical scrollbars appear when needed. |
|
JScrollPane(java.awt.Component 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. |
|
JScrollPane(java.awt.Component view,
int vsbPolicy,
int hsbPolicy)
Creates a JScrollPane that displays the view
component in a viewport
whose view position can be controlled with a pair of scrollbars. |
|
JScrollPane(int vsbPolicy,
int hsbPolicy)
Creates an empty (no viewport view) JScrollPane
with specified
scrollbar policies. |
Method Summary | |
JScrollBar |
createHorizontalScrollBar()
Returns a JScrollPane.ScrollBar by default. |
JScrollBar |
createVerticalScrollBar()
Returns a JScrollPane.ScrollBar by default. |
protected JViewport |
createViewport()
Returns a new JViewport by default. |
JViewport |
getColumnHeader()
Returns the column header. |
java.awt.Component |
getCorner(java.lang.String key)
Returns the component at the specified corner. |
JScrollBar |
getHorizontalScrollBar()
Returns the horizontal scroll bar that controls the viewport's horizontal view position. |
int |
getHorizontalScrollBarPolicy()
Returns the horizontal scroll bar policy value. |
JViewport |
getRowHeader()
Returns the row header. |
JScrollBar |
getVerticalScrollBar()
Returns the vertical scroll bar that controls the viewports vertical view position. |
int |
getVerticalScrollBarPolicy()
Returns the vertical scroll bar policy value. |
JViewport |
getViewport()
Returns the current JViewport . |
Border |
getViewportBorder()
Returns the Border object that surrounds the viewport. |
Rectangle |
getViewportBorderBounds()
Returns the bounds of the viewport's border. |
boolean |
isValidateRoot()
Calls revalidate on any descendant of this
JScrollPane . |
boolean |
isWheelScrollingEnabled()
Indicates whether or not scrolling will take place in response to the mouse wheel. |
void |
paint(java.awt.Graphics g)
Invoked by Swing to draw components. |
protected void |
paintBorder(java.awt.Graphics g)
Paints the component's border. |
protected void |
paintComponent(java.awt.Graphics g)
Calls the UI delegate's paint method, if the UI delegate is non- null . |
void |
paintComponents(java.awt.Graphics g)
|
protected java.lang.String |
paramString()
Returns a string representation of this JScrollPane . |
protected void |
printBorder(java.awt.Graphics g)
Prints the component's border. |
protected void |
printComponent(java.awt.Graphics g)
This is invoked during a printing operation. |
void |
printComponents(java.awt.Graphics g)
|
void |
setColumnHeader(JViewport columnHeader)
Removes the old columnHeader, if it exists. |
void |
setColumnHeaderView(java.awt.Component view)
Creates a column-header viewport if necessary, sets its view, and then adds the column-header viewport to the scrollpane. |
void |
setCorner(java.lang.String key,
java.awt.Component corner)
Adds a child that will appear in one of the scroll panes corners, if there's room. |
void |
setHorizontalScrollBar(JScrollBar horizontalScrollBar)
Adds the scrollbar that controls the viewport's horizontal view position to the scrollpane. |
void |
setHorizontalScrollBarPolicy(int policy)
Determines when the horizontal scrollbar appears in the scrollpane. |
void |
setLayout(java.awt.LayoutManager layout)
Sets the layout manager for this JScrollPane . |
void |
setRowHeader(JViewport rowHeader)
Removes the old rowHeader, if it exists. |
void |
setRowHeaderView(java.awt.Component view)
Creates a row-header viewport if necessary, sets its view and then adds the row-header viewport to the scrollpane. |
void |
setVerticalScrollBar(JScrollBar verticalScrollBar)
Adds the scrollbar that controls the viewports vertical view position to the scrollpane. |
void |
setVerticalScrollBarPolicy(int policy)
Determines when the vertical scrollbar appears in the scrollpane. |
void |
setViewport(JViewport 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. |
void |
setViewportBorder(Border viewportBorder)
Adds a border around the viewport. |
void |
setViewportView(java.awt.Component view)
Creates a viewport if necessary and then sets its view. |
void |
setWheelScrollingEnabled(boolean handleWheel)
Enables/disables scrolling in response to movement of the mouse wheel. |
Methods inherited from class java.awt.Container |
add, add, add, add, add, addContainerListener, addImpl, areFocusTraversalKeysSet, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, list, list, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, transferFocusBackward, transferFocusDownCycle, validate, validateTree |
Methods inherited from class java.awt.Component |
addComponentListener, addFocusListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentListeners, getCursor, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getSize, getToolkit, getTreeLock, hasFocus, imageUpdate, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isShowing, isValid, isVisible, list, list, list, paintAll, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processInputMethodEvent, processMouseEvent, processMouseWheelEvent, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, setBounds, setBounds, setCursor, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, toString, transferFocus, transferFocusUpCycle |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
protected int verticalScrollBarPolicy
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED
.
setVerticalScrollBarPolicy(int)
protected int horizontalScrollBarPolicy
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
.
setHorizontalScrollBarPolicy(int)
protected JViewport viewport
JViewport
.
setViewport(javax.swing.JViewport)
protected JScrollBar verticalScrollBar
JScrollBar
.
setVerticalScrollBar(javax.swing.JScrollBar)
protected JScrollBar horizontalScrollBar
JScrollBar
.
setHorizontalScrollBar(javax.swing.JScrollBar)
protected JViewport rowHeader
null
.
setRowHeader(javax.swing.JViewport)
protected JViewport columnHeader
null
.
setColumnHeader(javax.swing.JViewport)
protected java.awt.Component lowerLeft
null
.
setCorner(java.lang.String, java.awt.Component)
protected java.awt.Component lowerRight
null
.
setCorner(java.lang.String, java.awt.Component)
protected java.awt.Component upperLeft
null
.
setCorner(java.lang.String, java.awt.Component)
protected java.awt.Component upperRight
null
.
setCorner(java.lang.String, java.awt.Component)
Constructor Detail |
public JScrollPane(java.awt.Component view, int vsbPolicy, int hsbPolicy)
JScrollPane
that displays the view
component in a viewport
whose view position can be controlled with a pair of scrollbars.
The scrollbar policies specify when the scrollbars are displayed,
For example, if vsbPolicy
is
VERTICAL_SCROLLBAR_AS_NEEDED
then the vertical scrollbar only appears if the view doesn't fit
vertically. The available policy settings are listed at
setVerticalScrollBarPolicy(int)
and
setHorizontalScrollBarPolicy(int)
.
view
- the component to display in the scrollpanes viewportvsbPolicy
- an integer that specifies the vertical
scrollbar policyhsbPolicy
- an integer that specifies the horizontal
scrollbar policysetViewportView(java.awt.Component)
public JScrollPane(java.awt.Component view)
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.
view
- the component to display in the scrollpane's viewportsetViewportView(java.awt.Component)
public JScrollPane(int vsbPolicy, int hsbPolicy)
JScrollPane
with specified
scrollbar policies. The available policy settings are listed at
setVerticalScrollBarPolicy(int)
and
setHorizontalScrollBarPolicy(int)
.
vsbPolicy
- an integer that specifies the vertical
scrollbar policyhsbPolicy
- an integer that specifies the horizontal
scrollbar policysetViewportView(java.awt.Component)
public JScrollPane()
JScrollPane
where both horizontal and vertical scrollbars appear when needed.
Method Detail |
public final void paint(java.awt.Graphics g)
JComponent
paint
directly,
but should instead use the repaint
method to
schedule the component for redrawing.
This method actually delegates the work of painting to three
protected methods: paintComponent
,
paintBorder
,
and paintChildren
. They're called in the order
listed to ensure that children appear on top of component itself.
Generally speaking, the component and its children should not
paint in the insets area allocated to the border. Subclasses can
just override this method, as always.
paint
in class JComponent
g
- the Graphics
context in which to paintJComponent.paintBorder(java.awt.Graphics)
,
JComponent.paintChildren(java.awt.Graphics)
,
JComponent.getComponentGraphics(java.awt.Graphics)
,
JComponent.repaint(long, int, int, int, int)
public void setLayout(java.awt.LayoutManager layout)
JScrollPane
.
This method overrides setLayout
in
java.awt.Container
to ensure that only
LayoutManager
s which
are subclasses of ScrollPaneLayout
can be used in a
JScrollPane
. If layout
is non-null, this
will invoke syncWithScrollPane
on it.
layout
- the specified layout manager
java.lang.ClassCastException
- if layout is not a
ScrollPaneLayout
Container.getLayout()
,
Container.setLayout(java.awt.LayoutManager)
public boolean isValidateRoot()
revalidate
on any descendant of this
JScrollPane
. For example,
the viewport's view, will cause a request to be queued that
will validate the JScrollPane
and all its descendants.
isValidateRoot
in class JComponent
JComponent.revalidate()
public int getVerticalScrollBarPolicy()
verticalScrollBarPolicy
propertysetVerticalScrollBarPolicy(int)
public void setVerticalScrollBarPolicy(int policy)
policy
- one of the three values listed above
java.lang.IllegalArgumentException
- if policy
is not one of the legal values shown abovegetVerticalScrollBarPolicy()
public int getHorizontalScrollBarPolicy()
horizontalScrollBarPolicy
propertysetHorizontalScrollBarPolicy(int)
public void setHorizontalScrollBarPolicy(int policy)
policy
- one of the three values listed above
java.lang.IllegalArgumentException
- if policy
is not one of the legal values shown abovegetHorizontalScrollBarPolicy()
public Border getViewportBorder()
Border
object that surrounds the viewport.
viewportBorder
propertysetViewportBorder(javax.swing.border.Border)
public void setViewportBorder(Border viewportBorder)
JViewport
doesn't support
the JComponent
border property.
Similarly setting the JScrollPane
s
viewport doesn't affect the viewportBorder
property.
The default value of this property is computed by the look and feel implementation.
viewportBorder
- the border to be addedgetViewportBorder()
,
setViewport(javax.swing.JViewport)
public Rectangle getViewportBorderBounds()
Rectangle
object specifying the viewport borderpublic JScrollBar createHorizontalScrollBar()
JScrollPane.ScrollBar
by default.
JScrollBar
with a horizontal orientationJScrollBar
public JScrollBar getHorizontalScrollBar()
horizontalScrollBar
propertysetHorizontalScrollBar(javax.swing.JScrollBar)
public void setHorizontalScrollBar(JScrollBar horizontalScrollBar)
JScrollPane
creates
horizontal and vertical scrollbars by default.
horizontalScrollBar
- the horizontal scrollbar to be addedcreateHorizontalScrollBar()
,
getHorizontalScrollBar()
public JScrollBar createVerticalScrollBar()
JScrollPane.ScrollBar
by default.
JScrollBar
with a vertical orientationJScrollBar
public JScrollBar getVerticalScrollBar()
verticalScrollBar
propertysetVerticalScrollBar(javax.swing.JScrollBar)
public void setVerticalScrollBar(JScrollBar verticalScrollBar)
JScrollPane
creates vertical and
horizontal scrollbars by default.
verticalScrollBar
- the new vertical scrollbar to be addedcreateVerticalScrollBar()
,
getVerticalScrollBar()
protected JViewport createViewport()
JViewport
by default.
Used to create the
viewport (as needed) in setViewportView
,
setRowHeaderView
, and setColumnHeaderView
.
Subclasses may override this method to return a subclass of
JViewport
.
JViewport
public JViewport getViewport()
JViewport
.
viewport
propertysetViewport(javax.swing.JViewport)
public void setViewport(JViewport viewport)
Most applications will find it more convenient to use
setViewportView
to add a viewport and a view to the scrollpane.
viewport
- the new viewport to be used; if viewport is
null
, the old viewport is still removed
and the new viewport is set to null
createViewport()
,
getViewport()
,
setViewportView(java.awt.Component)
public void setViewportView(java.awt.Component view)
JScrollPane
constructor
should use this method to specify the scrollable child that's going
to be displayed in the scrollpane. For example:
JScrollPane scrollpane = new JScrollPane(); scrollpane.setViewportView(myBigComponentToScroll);Applications should not add children directly to the scrollpane.
view
- the component to add to the viewportsetViewport(javax.swing.JViewport)
,
JViewport.setView(java.awt.Component)
public JViewport getRowHeader()
rowHeader
propertysetRowHeader(javax.swing.JViewport)
public void setRowHeader(JViewport rowHeader)
null
, syncs the y coordinate of its
viewPosition with
the viewport (if there is one) and then adds it to the scrollpane.
Most applications will find it more convenient to use
setRowHeaderView
to add a row header component and its viewport to the scrollpane.
rowHeader
- the new row header to be used; if null
the old row header is still removed and the new rowHeader
is set to null
getRowHeader()
,
setRowHeaderView(java.awt.Component)
public void setRowHeaderView(java.awt.Component view)
JScrollPane scrollpane = new JScrollPane(); scrollpane.setViewportView(myBigComponentToScroll); scrollpane.setRowHeaderView(myBigComponentsRowHeader);
view
- the component to display as the row headersetRowHeader(javax.swing.JViewport)
,
JViewport.setView(java.awt.Component)
public JViewport getColumnHeader()
columnHeader
propertysetColumnHeader(javax.swing.JViewport)
public void setColumnHeader(JViewport columnHeader)
null
, sync the x coordinate of the its viewPosition
with the viewport (if there is one) and then add it to the scrollpane.
Most applications will find it more convenient to use
setRowHeaderView
to add a row header component and its viewport to the scrollpane.
getColumnHeader()
,
setColumnHeaderView(java.awt.Component)
public void setColumnHeaderView(java.awt.Component view)
JScrollPane scrollpane = new JScrollPane(); scrollpane.setViewportView(myBigComponentToScroll); scrollpane.setColumnHeaderView(myBigComponentsColumnHeader);
view
- the component to display as the column headersetColumnHeader(javax.swing.JViewport)
,
JViewport.setView(java.awt.Component)
public java.awt.Component getCorner(java.lang.String key)
key
value specifying the corner is one of:
key
- one of the values as shown above
null
if key
is invalid:
setCorner(java.lang.String, java.awt.Component)
public void setCorner(java.lang.String key, java.awt.Component corner)
Although "corner" doesn't match any beans property
signature, PropertyChange
events are generated with the
property name set to the corner key.
key
- identifies which corner the component will appear incorner
- one of the following components:
java.lang.IllegalArgumentException
- if corner key is invalidpublic boolean isWheelScrollingEnabled()
setWheelScrollingEnabled(boolean)
public void setWheelScrollingEnabled(boolean handleWheel)
handleWheel
- true
if scrolling should be done
automatically for a MouseWheelEvent,
false
otherwise.isWheelScrollingEnabled()
,
MouseWheelEvent
,
MouseWheelListener
protected java.lang.String paramString()
JScrollPane
.
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
be null
.
paramString
in class JComponent
JScrollPane
.protected final void paintComponent(java.awt.Graphics g)
JComponent
null
. We pass the delegate a copy of the
Graphics
object to protect the rest of the
paint code from irrevocable changes
(for example, Graphics.translate
).
paintComponent
in class JComponent
g
- the Graphics
object to protectJComponent.paint(java.awt.Graphics)
,
ComponentUI
protected final void paintBorder(java.awt.Graphics g)
JComponent
paintBorder
in class JComponent
g
- the Graphics
context in which to paintJComponent.paint(java.awt.Graphics)
,
JComponent.setBorder(javax.swing.border.Border)
public final void paintComponents(java.awt.Graphics g)
protected final void printComponent(java.awt.Graphics g)
JComponent
paintComponent
on the component.
printComponent
in class JComponent
g
- the Graphics
context in which to paintJComponent.print(java.awt.Graphics)
protected final void printBorder(java.awt.Graphics g)
JComponent
paintBorder
on the component.
printBorder
in class JComponent
g
- the Graphics
context in which to paintJComponent.print(java.awt.Graphics)
public final void printComponents(java.awt.Graphics g)
|
JSR-209 (Final Release) | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |