Class ScrollPane
- All Implemented Interfaces:
-
ImageObserver
,MenuContainer
,Serializable
,Accessible
public class ScrollPane extends Container implements Accessible
- as needed: scrollbars created and shown only when needed by scrollpane
- always: scrollbars created and always shown by the scrollpane
- 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:
Nested Class Summary
Modifier and Type | Class | Description |
---|---|---|
protected class |
ScrollPane.AccessibleAWTScrollPane |
This class implements accessibility support for the ScrollPane class. |
Nested classes/interfaces declared in class java.awt.Container
Container.AccessibleAWTContainer
Nested classes/interfaces declared in class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
Field Summary
Modifier and Type | Field | Description |
---|---|---|
static final int |
SCROLLBARS_ALWAYS |
Specifies that horizontal/vertical scrollbars should always be shown regardless of the respective sizes of the scrollpane and child. |
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. |
static final int |
SCROLLBARS_NEVER |
Specifies that horizontal/vertical scrollbars should never be shown regardless of the respective sizes of the scrollpane and child. |
Fields declared in class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
Fields declared in interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Constructor Summary
Constructor | Description |
---|---|
ScrollPane() |
Create a new scrollpane container with a scrollbar display policy of "as needed". |
ScrollPane |
Create a new scrollpane container. |
Method Summary
Modifier and Type | Method | Description |
---|---|---|
protected final void |
addImpl |
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 |
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. |
String |
paramString() |
Returns a string representing the state of this ScrollPane . |
void |
printComponents |
Prints the component in this scroll pane. |
protected void |
processMouseWheelEvent |
Process mouse wheel events that are delivered to this ScrollPane by scrolling an appropriate amount. |
final void |
setLayout |
Sets the layout manager for this container. |
void |
setScrollPosition |
Scrolls to the specified position within the child component. |
void |
setScrollPosition |
Scrolls to the specified position within the child component. |
void |
setWheelScrollingEnabled |
Enables/disables scrolling in response to movement of the mouse wheel. |
Methods declared in class java.awt.Container
add, add, add, add, add, addContainerListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getListeners, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, isValidateRoot, list, list, locate, minimumSize, paint, paintComponents, preferredSize, print, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, transferFocusDownCycle, update, validate, validateTree
Methods declared in class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBaseline, getBaselineResizeBehavior, 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, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, revalidate, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setMixingCutoutShape, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
Field Details
SCROLLBARS_AS_NEEDED
public static final int SCROLLBARS_AS_NEEDED
- See Also:
SCROLLBARS_ALWAYS
public static final int SCROLLBARS_ALWAYS
- See Also:
SCROLLBARS_NEVER
public static final int SCROLLBARS_NEVER
- See Also:
Constructor Details
ScrollPane
public ScrollPane() throws HeadlessException
- Throws:
-
HeadlessException
- if GraphicsEnvironment.isHeadless() returns true - See Also:
ScrollPane
@ConstructorProperties("scrollbarDisplayPolicy") public ScrollPane(int scrollbarDisplayPolicy) throws HeadlessException
- Parameters:
-
scrollbarDisplayPolicy
- policy for when scrollbars should be shown - Throws:
-
IllegalArgumentException
- if the specified scrollbar display policy is invalid -
HeadlessException
- if GraphicsEnvironment.isHeadless() returns true - See Also:
Method Details
addImpl
protected final void addImpl(Component comp, Object constraints, int index)
getScrollbarDisplayPolicy
public int getScrollbarDisplayPolicy()
- Returns:
- the display policy for the scrollbars
getViewportSize
public Dimension getViewportSize()
- Returns:
- the size of the view port in pixels
getHScrollbarHeight
public int getHScrollbarHeight()
- Returns:
- the height of a horizontal scrollbar in pixels
getVScrollbarWidth
public int getVScrollbarWidth()
- Returns:
- the width of a vertical scrollbar in pixels
getVAdjustable
public Adjustable getVAdjustable()
ScrollPaneAdjustable
object which represents the state of the vertical scrollbar. The declared return type of this method is Adjustable
to maintain backward compatibility.- Returns:
- the vertical scrollbar state
- See Also:
getHAdjustable
public Adjustable getHAdjustable()
ScrollPaneAdjustable
object which represents the state of the horizontal scrollbar. The declared return type of this method is Adjustable
to maintain backward compatibility.- Returns:
- the horizontal scrollbar state
- See Also:
setScrollPosition
public void setScrollPosition(int x, int y)
- Parameters:
-
x
- the x position to scroll to -
y
- the y position to scroll to - Throws:
-
NullPointerException
- if the scrollpane does not contain a child
setScrollPosition
public void setScrollPosition(Point p)
- Parameters:
-
p
- the Point representing the position to scroll to - Throws:
-
NullPointerException
- ifp
isnull
getScrollPosition
public Point getScrollPosition()
- Returns:
- the coordinate position for the current scroll position
- Throws:
-
NullPointerException
- if the scrollpane does not contain a child
setLayout
public final void setLayout(LayoutManager mgr)
doLayout
public void doLayout()
layout
@Deprecated public void layout()
doLayout()
.printComponents
public void printComponents(Graphics g)
- Overrides:
-
printComponents
in classContainer
- Parameters:
-
g
- the specified Graphics window - See Also:
addNotify
public void addNotify()
paramString
public String paramString()
ScrollPane
. 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
in classContainer
- Returns:
- the parameter string of this scroll pane
processMouseWheelEvent
protected void processMouseWheelEvent(MouseWheelEvent e)
ScrollPane
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
in classComponent
- Parameters:
-
e
- the mouse wheel event - Since:
- 1.4
- See Also:
eventTypeEnabled
protected boolean eventTypeEnabled(int type)
- Since:
- 1.4
setWheelScrollingEnabled
public void setWheelScrollingEnabled(boolean handleWheel)
- Parameters:
-
handleWheel
-true
if scrolling should be done automatically for a MouseWheelEvent,false
otherwise. - Since:
- 1.4
- See Also:
isWheelScrollingEnabled
public boolean isWheelScrollingEnabled()
- Returns:
-
true
if the wheel scrolling enabled; otherwisefalse
- Since:
- 1.4
- See Also:
getAccessibleContext
public AccessibleContext getAccessibleContext()
- Specified by:
-
getAccessibleContext
in interfaceAccessible
- Overrides:
-
getAccessibleContext
in classComponent
- Returns:
- an AccessibleAWTScrollPane that serves as the AccessibleContext of this ScrollPane
- Since:
- 1.3
© 1993, 2021, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/java/awt/ScrollPane.html
doLayout()
.