ScrollPane (Java 2 Platform SE 5.0) (original) (raw)


java.awt

Class ScrollPane

java.lang.Object extended by java.awt.Component extended by java.awt.Container extended by java.awt.ScrollPane

All Implemented Interfaces:

ImageObserver, MenuContainer, Serializable, Accessible


public class ScrollPane

extends Container

implements Accessible

A container class which implements automatic horizontal and/or vertical scrolling for a single child component. The display policy for the scrollbars can be set to:

  1. as needed: scrollbars created and shown only when needed by scrollpane
  2. always: scrollbars created and always shown by the scrollpane
  3. never: scrollbars never created or shown by the scrollpane

The state of the horizontal and vertical scrollbars is represented by two ScrollPaneAdjustable objects (one for each dimension) which implement the Adjustable interface. The API provides methods to access those objects such that the attributes on the Adjustable object (such as unitIncrement, value, etc.) can be manipulated.

Certain adjustable properties (minimum, maximum, blockIncrement, and visibleAmount) are set internally by the scrollpane in accordance with the geometry of the scrollpane and its child and these should not be set by programs using the scrollpane.

If the scrollbar display policy is defined as "never", then the scrollpane can still be programmatically scrolled using the setScrollPosition() method and the scrollpane will move and clip the child's contents appropriately. This policy is useful if the program needs to create and manage its own adjustable controls.

The placement of the scrollbars is controlled by platform-specific properties set by the user outside of the program.

The initial size of this container is set to 100x100, but can be reset using setSize().

Scrolling with the wheel on a wheel-equipped mouse is enabled by default. This can be disabled using setWheelScrollingEnabled. Wheel scrolling can be customized by setting the block and unit increment of the horizontal and vertical Adjustables. For information on how mouse wheel events are dispatched, see the class description for MouseWheelEvent.

Insets are used to define any space used by scrollbars and any borders created by the scroll pane. getInsets() can be used to get the current value for the insets. If the value of scrollbarsAlwaysVisible is false, then the value of the insets will change dynamically depending on whether the scrollbars are currently visible or not.

See Also:

Serialized Form


Nested Class Summary
protected class ScrollPane.AccessibleAWTScrollPane This class implements accessibility support for the ScrollPane class.
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
Field Summary
static int SCROLLBARS_ALWAYS Specifies that horizontal/vertical scrollbars should always be shown regardless of the respective sizes of the scrollpane and child.
static int SCROLLBARS_AS_NEEDED Specifies that horizontal/vertical scrollbar should be shown only when the size of the child exceeds the size of the scrollpane in the horizontal/vertical dimension.
static int SCROLLBARS_NEVER Specifies that horizontal/vertical scrollbars should never be shown regardless of the respective sizes of the scrollpane and child.
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
ScrollPane() Create a new scrollpane container with a scrollbar display policy of "as needed".
ScrollPane(int scrollbarDisplayPolicy) Create a new scrollpane container.
Method Summary
protected void [addImpl](../../java/awt/ScrollPane.html#addImpl%28java.awt.Component, java.lang.Object, int%29)(Component comp,Object constraints, int index) Adds the specified component to this scroll pane container.
void addNotify() Creates the scroll pane's peer.
void doLayout() Lays out this container by resizing its child to its preferred size.
protected boolean eventTypeEnabled(int type) If wheel scrolling is enabled, we return true for MouseWheelEvents
AccessibleContext getAccessibleContext() Gets the AccessibleContext associated with this ScrollPane.
Adjustable getHAdjustable() Returns the ScrollPaneAdjustable object which represents the state of the horizontal scrollbar.
int getHScrollbarHeight() Returns the height that would be occupied by a horizontal scrollbar, which is independent of whether it is currently displayed by the scroll pane or not.
int getScrollbarDisplayPolicy() Returns the display policy for the scrollbars.
Point getScrollPosition() Returns the current x,y position within the child which is displayed at the 0,0 location of the scrolled panel's view port.
Adjustable getVAdjustable() Returns the ScrollPaneAdjustable object which represents the state of the vertical scrollbar.
Dimension getViewportSize() Returns the current size of the scroll pane's view port.
int getVScrollbarWidth() Returns the width that would be occupied by a vertical scrollbar, which is independent of whether it is currently displayed by the scroll pane or not.
boolean isWheelScrollingEnabled() Indicates whether or not scrolling will take place in response to the mouse wheel.
void layout() Deprecated. As of JDK version 1.1, replaced by doLayout().
String paramString() Returns a string representing the state of thisScrollPane.
void printComponents(Graphics g) Prints the component in this scroll pane.
protected void processMouseWheelEvent(MouseWheelEvent e) Process mouse wheel events that are delivered to thisScrollPane by scrolling an appropriate amount.
void setLayout(LayoutManager mgr) Sets the layout manager for this container.
void [setScrollPosition](../../java/awt/ScrollPane.html#setScrollPosition%28int, int%29)(int x, int y) Scrolls to the specified position within the child component.
void setScrollPosition(Point p) Scrolls to the specified position within the child component.
void setWheelScrollingEnabled(boolean handleWheel) Enables/disables scrolling in response to movement of the mouse wheel.
Methods inherited from class java.awt.Container
add, [add](../../java/awt/Container.html#add%28java.awt.Component, int%29), [add](../../java/awt/Container.html#add%28java.awt.Component, java.lang.Object%29), [add](../../java/awt/Container.html#add%28java.awt.Component, java.lang.Object, int%29), [add](../../java/awt/Container.html#add%28java.lang.String, java.awt.Component%29), addContainerListener, addPropertyChangeListener, [addPropertyChangeListener](../../java/awt/Container.html#addPropertyChangeListener%28java.lang.String, java.beans.PropertyChangeListener%29), applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, [findComponentAt](../../java/awt/Container.html#findComponentAt%28int, int%29), findComponentAt, getAlignmentX, getAlignmentY, getComponent, [getComponentAt](../../java/awt/Container.html#getComponentAt%28int, int%29), getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getListeners, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, [list](../../java/awt/Container.html#list%28java.io.PrintStream, int%29), [list](../../java/awt/Container.html#list%28java.io.PrintWriter, int%29), [locate](../../java/awt/Container.html#locate%28int, int%29), minimumSize, paint, paintComponents, preferredSize, print, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, [setComponentZOrder](../../java/awt/Container.html#setComponentZOrder%28java.awt.Component, int%29), setFocusCycleRoot, [setFocusTraversalKeys](../../java/awt/Container.html#setFocusTraversalKeys%28int, java.util.Set%29), setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, transferFocusBackward, transferFocusDownCycle, update, validate, validateTree
Methods inherited from class java.awt.Component
[action](../../java/awt/Component.html#action%28java.awt.Event, java.lang.Object%29), add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, [checkImage](../../java/awt/Component.html#checkImage%28java.awt.Image, java.awt.image.ImageObserver%29), [checkImage](../../java/awt/Component.html#checkImage%28java.awt.Image, int, int, java.awt.image.ImageObserver%29), [coalesceEvents](../../java/awt/Component.html#coalesceEvents%28java.awt.AWTEvent, java.awt.AWTEvent%29), [contains](../../java/awt/Component.html#contains%28int, int%29), contains, createImage, [createImage](../../java/awt/Component.html#createImage%28int, int%29), [createVolatileImage](../../java/awt/Component.html#createVolatileImage%28int, int%29), [createVolatileImage](../../java/awt/Component.html#createVolatileImage%28int, int, java.awt.ImageCapabilities%29), disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, boolean, boolean%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, byte, byte%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, char, char%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, double, double%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, float, float%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, int, int%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, long, long%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, java.lang.Object, java.lang.Object%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, short, short%29), getBackground, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, [gotFocus](../../java/awt/Component.html#gotFocus%28java.awt.Event, java.lang.Object%29), handleEvent, hasFocus, hide, [imageUpdate](../../java/awt/Component.html#imageUpdate%28java.awt.Image, int, int, int, int, int%29), [inside](../../java/awt/Component.html#inside%28int, int%29), isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, [keyDown](../../java/awt/Component.html#keyDown%28java.awt.Event, int%29), [keyUp](../../java/awt/Component.html#keyUp%28java.awt.Event, int%29), list, list, list, location, [lostFocus](../../java/awt/Component.html#lostFocus%28java.awt.Event, java.lang.Object%29), [mouseDown](../../java/awt/Component.html#mouseDown%28java.awt.Event, int, int%29), [mouseDrag](../../java/awt/Component.html#mouseDrag%28java.awt.Event, int, int%29), [mouseEnter](../../java/awt/Component.html#mouseEnter%28java.awt.Event, int, int%29), [mouseExit](../../java/awt/Component.html#mouseExit%28java.awt.Event, int, int%29), [mouseMove](../../java/awt/Component.html#mouseMove%28java.awt.Event, int, int%29), [mouseUp](../../java/awt/Component.html#mouseUp%28java.awt.Event, int, int%29), [move](../../java/awt/Component.html#move%28int, int%29), nextFocus, paintAll, postEvent, [prepareImage](../../java/awt/Component.html#prepareImage%28java.awt.Image, java.awt.image.ImageObserver%29), [prepareImage](../../java/awt/Component.html#prepareImage%28java.awt.Image, int, int, java.awt.image.ImageObserver%29), printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, [removePropertyChangeListener](../../java/awt/Component.html#removePropertyChangeListener%28java.lang.String, java.beans.PropertyChangeListener%29), repaint, [repaint](../../java/awt/Component.html#repaint%28int, int, int, int%29), repaint, [repaint](../../java/awt/Component.html#repaint%28long, int, int, int, int%29), requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, [reshape](../../java/awt/Component.html#reshape%28int, int, int, int%29), resize, [resize](../../java/awt/Component.html#resize%28int, int%29), setBackground, [setBounds](../../java/awt/Component.html#setBounds%28int, int, int, int%29), setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, [setLocation](../../java/awt/Component.html#setLocation%28int, int%29), setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, [setSize](../../java/awt/Component.html#setSize%28int, int%29), setVisible, show, show, size, toString, transferFocus, transferFocusUpCycle
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, [wait](../../java/lang/Object.html#wait%28long, int%29)
Field Detail

SCROLLBARS_AS_NEEDED

public static final int SCROLLBARS_AS_NEEDED

Specifies that horizontal/vertical scrollbar should be shown only when the size of the child exceeds the size of the scrollpane in the horizontal/vertical dimension.

See Also:

Constant Field Values


SCROLLBARS_ALWAYS

public static final int SCROLLBARS_ALWAYS

Specifies that horizontal/vertical scrollbars should always be shown regardless of the respective sizes of the scrollpane and child.

See Also:

Constant Field Values


SCROLLBARS_NEVER

public static final int SCROLLBARS_NEVER

Specifies that horizontal/vertical scrollbars should never be shown regardless of the respective sizes of the scrollpane and child.

See Also:

Constant Field Values

Constructor Detail

ScrollPane

public ScrollPane() throws HeadlessException

Create a new scrollpane container with a scrollbar display policy of "as needed".

Throws:

[HeadlessException](../../java/awt/HeadlessException.html "class in java.awt") - if GraphicsEnvironment.isHeadless() returns true

See Also:

GraphicsEnvironment.isHeadless()


ScrollPane

public ScrollPane(int scrollbarDisplayPolicy) throws HeadlessException

Create a new scrollpane container.

Parameters:

scrollbarDisplayPolicy - policy for when scrollbars should be shown

Throws:

[IllegalArgumentException](../../java/lang/IllegalArgumentException.html "class in java.lang") - if the specified scrollbar display policy is invalid

[HeadlessException](../../java/awt/HeadlessException.html "class in java.awt") - if GraphicsEnvironment.isHeadless() returns true

See Also:

GraphicsEnvironment.isHeadless()

Method Detail

addImpl

protected final void addImpl(Component comp, Object constraints, int index)

Adds the specified component to this scroll pane container. If the scroll pane has an existing child component, that component is removed and the new one is added.

Overrides:

[addImpl](../../java/awt/Container.html#addImpl%28java.awt.Component, java.lang.Object, int%29) in class [Container](../../java/awt/Container.html "class in java.awt")

Parameters:

comp - the component to be added

constraints - not applicable

index - position of child component (must be <= 0)

See Also:

Container.add(Component), [Container.add(Component, int)](../../java/awt/Container.html#add%28java.awt.Component, int%29), [Container.add(Component, java.lang.Object)](../../java/awt/Container.html#add%28java.awt.Component, java.lang.Object%29), LayoutManager, LayoutManager2


getScrollbarDisplayPolicy

public int getScrollbarDisplayPolicy()

Returns the display policy for the scrollbars.

Returns:

the display policy for the scrollbars


getViewportSize

public Dimension getViewportSize()

Returns the current size of the scroll pane's view port.

Returns:

the size of the view port in pixels


getHScrollbarHeight

public int getHScrollbarHeight()

Returns the height that would be occupied by a horizontal scrollbar, which is independent of whether it is currently displayed by the scroll pane or not.

Returns:

the height of a horizontal scrollbar in pixels


getVScrollbarWidth

public int getVScrollbarWidth()

Returns the width that would be occupied by a vertical scrollbar, which is independent of whether it is currently displayed by the scroll pane or not.

Returns:

the width of a vertical scrollbar in pixels


getVAdjustable

public Adjustable getVAdjustable()

Returns the ScrollPaneAdjustable object which represents the state of the vertical scrollbar. The declared return type of this method isAdjustable to maintain backward compatibility.

See Also:

ScrollPaneAdjustable


getHAdjustable

public Adjustable getHAdjustable()

Returns the ScrollPaneAdjustable object which represents the state of the horizontal scrollbar. The declared return type of this method isAdjustable to maintain backward compatibility.

See Also:

ScrollPaneAdjustable


setScrollPosition

public void setScrollPosition(int x, int y)

Scrolls to the specified position within the child component. A call to this method is only valid if the scroll pane contains a child. Specifying a position outside of the legal scrolling bounds of the child will scroll to the closest legal position. Legal bounds are defined to be the rectangle: x = 0, y = 0, width = (child width - view port width), height = (child height - view port height). This is a convenience method which interfaces with the Adjustable objects which represent the state of the scrollbars.

Parameters:

x - the x position to scroll to

y - the y position to scroll to

Throws:

[NullPointerException](../../java/lang/NullPointerException.html "class in java.lang") - if the scrollpane does not contain a child


setScrollPosition

public void setScrollPosition(Point p)

Scrolls to the specified position within the child component. A call to this method is only valid if the scroll pane contains a child and the specified position is within legal scrolling bounds of the child. Specifying a position outside of the legal scrolling bounds of the child will scroll to the closest legal position. Legal bounds are defined to be the rectangle: x = 0, y = 0, width = (child width - view port width), height = (child height - view port height). This is a convenience method which interfaces with the Adjustable objects which represent the state of the scrollbars.

Parameters:

p - the Point representing the position to scroll to


getScrollPosition

public Point getScrollPosition()

Returns the current x,y position within the child which is displayed at the 0,0 location of the scrolled panel's view port. This is a convenience method which interfaces with the adjustable objects which represent the state of the scrollbars.

Returns:

the coordinate position for the current scroll position

Throws:

[NullPointerException](../../java/lang/NullPointerException.html "class in java.lang") - if the scrollpane does not contain a child


setLayout

public final void setLayout(LayoutManager mgr)

Sets the layout manager for this container. This method is overridden to prevent the layout mgr from being set.

Overrides:

[setLayout](../../java/awt/Container.html#setLayout%28java.awt.LayoutManager%29) in class [Container](../../java/awt/Container.html "class in java.awt")

Parameters:

mgr - the specified layout manager

See Also:

Container.doLayout(), Container.getLayout()


doLayout

public void doLayout()

Lays out this container by resizing its child to its preferred size. If the new preferred size of the child causes the current scroll position to be invalid, the scroll position is set to the closest valid position.

Overrides:

[doLayout](../../java/awt/Container.html#doLayout%28%29) in class [Container](../../java/awt/Container.html "class in java.awt")

See Also:

Component.validate()


layout

@Deprecated public void layout()

Deprecated. As of JDK version 1.1, replaced by doLayout().

Overrides:

[layout](../../java/awt/Container.html#layout%28%29) in class [Container](../../java/awt/Container.html "class in java.awt")


printComponents

public void printComponents(Graphics g)

Prints the component in this scroll pane.

Overrides:

[printComponents](../../java/awt/Container.html#printComponents%28java.awt.Graphics%29) in class [Container](../../java/awt/Container.html "class in java.awt")

Parameters:

g - the specified Graphics window

See Also:

Component.print(java.awt.Graphics), Component.printAll(java.awt.Graphics)


addNotify

public void addNotify()

Creates the scroll pane's peer.

Overrides:

[addNotify](../../java/awt/Container.html#addNotify%28%29) in class [Container](../../java/awt/Container.html "class in java.awt")

See Also:

Component.isDisplayable(), Container.removeNotify()


paramString

public String paramString()

Returns a string representing the state of thisScrollPane. 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.

Overrides:

[paramString](../../java/awt/Container.html#paramString%28%29) in class [Container](../../java/awt/Container.html "class in java.awt")

Returns:

the parameter string of this scroll pane


processMouseWheelEvent

protected void processMouseWheelEvent(MouseWheelEvent e)

Process mouse wheel events that are delivered to thisScrollPane by scrolling an appropriate amount.

Note that if the event parameter is null the behavior is unspecified and may result in an exception.

Overrides:

[processMouseWheelEvent](../../java/awt/Component.html#processMouseWheelEvent%28java.awt.event.MouseWheelEvent%29) in class [Component](../../java/awt/Component.html "class in java.awt")

Parameters:

e - the mouse wheel event

Since:

1.4

See Also:

MouseWheelEvent, MouseWheelListener, Component.addMouseWheelListener(java.awt.event.MouseWheelListener), Component.enableEvents(long)


eventTypeEnabled

protected boolean eventTypeEnabled(int type)

If wheel scrolling is enabled, we return true for MouseWheelEvents

Since:

1.4


setWheelScrollingEnabled

public void setWheelScrollingEnabled(boolean handleWheel)

Enables/disables scrolling in response to movement of the mouse wheel. Wheel scrolling is enabled by default.

Parameters:

handleWheel - true if scrolling should be done automatically for a MouseWheelEvent,false otherwise.

Since:

1.4

See Also:

isWheelScrollingEnabled(), MouseWheelEvent, MouseWheelListener


isWheelScrollingEnabled

public boolean isWheelScrollingEnabled()

Indicates whether or not scrolling will take place in response to the mouse wheel. Wheel scrolling is enabled by default.

Since:

1.4

See Also:

setWheelScrollingEnabled(boolean)


getAccessibleContext

public AccessibleContext getAccessibleContext()

Gets the AccessibleContext associated with this ScrollPane. For scroll panes, the AccessibleContext takes the form of an AccessibleAWTScrollPane. A new AccessibleAWTScrollPane instance is created if necessary.

Specified by:

[getAccessibleContext](../../javax/accessibility/Accessible.html#getAccessibleContext%28%29) in interface [Accessible](../../javax/accessibility/Accessible.html "interface in javax.accessibility")

Overrides:

[getAccessibleContext](../../java/awt/Component.html#getAccessibleContext%28%29) in class [Component](../../java/awt/Component.html "class in java.awt")

Returns:

an AccessibleAWTScrollPane that serves as the AccessibleContext of this ScrollPane



Submit a bug or feature
For further API reference and developer documentation, see Java 2 SDK SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.

Copyright © 2004, 2010 Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.