APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.awt

public Class Scrollbar

extends Component
implements Adjustable, Accessible
Class Inheritance
All Implemented Interfaces
javax.accessibility.Accessible, java.awt.Adjustable
Imports
java.awt.event.AdjustmentEvent, .AdjustmentListener, java.awt.peer.ScrollbarPeer, java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serial, java.util.EventListener, javax.accessibility.Accessible, .AccessibleContext, .AccessibleRole, .AccessibleState, .AccessibleStateSet, .AccessibleValue

The Scrollbar class embodies a scroll bar, a familiar user-interface object. A scroll bar provides a convenient means for allowing a user to select from a range of values. The following three vertical scroll bars could be used as slider controls to pick the red, green, and blue components of a color:

Image shows 3 vertical sliders,
side-by-side.

Each scroll bar in this example could be created with code similar to the following:


redSlider=new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 255);
add(redSlider);

Alternatively, a scroll bar can represent a range of values. For example, if a scroll bar is used for scrolling through text, the width of the "bubble" (also called the "thumb" or "scroll box") can be used to represent the amount of text that is visible. Here is an example of a scroll bar that represents a range:

Image shows horizontal slider with starting range of 0 and ending range
of 300. The slider thumb is labeled 60.

The value range represented by the bubble in this example is the visible amount. The horizontal scroll bar in this example could be created with code like the following:


ranger = new Scrollbar(Scrollbar.HORIZONTAL, 0, 60, 0, 300);
add(ranger);

Note that the actual maximum value of the scroll bar is the maximum minus the visible amount. In the previous example, because the maximum is 300 and the visible amount is 60, the actual maximum value is 240. The range of the scrollbar track is 0 - 300. The left side of the bubble indicates the value of the scroll bar.

Normally, the user changes the value of the scroll bar by making a gesture with the mouse. For example, the user can drag the scroll bar's bubble up and down, or click in the scroll bar's unit increment or block increment areas. Keyboard gestures can also be mapped to the scroll bar. By convention, the Page Up and Page Down keys are equivalent to clicking in the scroll bar's block increment and block decrement areas.

When the user changes the value of the scroll bar, the scroll bar receives an instance of AdjustmentEvent. The scroll bar processes this event, passing it along to any registered listeners.

Any object that wishes to be notified of changes to the scroll bar's value should implement AdjustmentListener, an interface defined in the package java.awt.event. Listeners can be added and removed dynamically by calling the methods addAdjustmentListener and removeAdjustmentListener.

The AdjustmentEvent class defines five types of adjustment event, listed here:

The JDK 1.0 event system is supported for backwards compatibility, but its use with newer versions of the platform is discouraged. The five types of adjustment events introduced with JDK 1.1 correspond to the five event types that are associated with scroll bars in previous platform versions. The following list gives the adjustment event type, and the corresponding JDK 1.0 event type it replaces.

Note

We recommend using a Scrollbar for value selection only. If you want to implement a scrollable component inside a container, we recommend you use a ScrollPane. If you use a Scrollbar for this purpose, you are likely to encounter issues with painting, key handling, sizing and positioning.

Author
Sami Shaio
Since
1.0
See Also
java.awt.event.AdjustmentEvent, java.awt.event.AdjustmentListener

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
Scrollbar.AccessibleAWTScrollBar

This class implements accessibility support for the Scrollbar class.

Field Summary

Modifier and TypeField and Description
pack-priv transient AdjustmentListener
private static final String
public static final int
HORIZONTAL

Hides java.awt.Adjustable.HORIZONTAL.

A constant that indicates a horizontal scroll bar.
pack-priv transient boolean
isAdjusting

The adjusting status of the Scrollbar.

pack-priv int
lineIncrement

The amount by which the scrollbar value will change when going up or down by a line.

pack-priv int
maximum

The maximum value of the Scrollbar.

pack-priv int
minimum

The minimum value of the Scrollbar.

private static int
pack-priv int
orientation

The Scrollbar's orientation--being either horizontal or vertical.

pack-priv int
pageIncrement

The amount by which the scrollbar value will change when going up or down by a page.

private int
scrollbarSerializedDataVersion

The scroll bar's serialized Data Version.

private static final long
serialVersionUID

Hides java.awt.Component.serialVersionUID.

Use serialVersionUID from JDK 1.1 for interoperability.
pack-priv int
value

The value of the Scrollbar.

public static final int
VERTICAL

Hides java.awt.Adjustable.VERTICAL.

A constant that indicates a vertical scroll bar.
pack-priv int
visibleAmount

The size of the Scrollbar's bubble.

Inherited from java.awt.Component:
accessibleContextactionListenerKadjustmentListenerKbackgroundbackgroundEraseDisabledBOTTOM_ALIGNMENTCENTER_ALIGNMENTcomponentListenercomponentListenerKcomponentOrientationcontainerListenerKcursordropTargetenabledeventCacheeventMaskfocusListenerfocusListenerKfocusTraversalKeysfontforegroundheighthierarchyBoundsListenerhierarchyBoundsListenerKhierarchyListenerhierarchyListenerKignoreRepaintincRateinputMethodListenerinputMethodListenerKisIncisPackeditemListenerKkeyListenerkeyListenerKLEFT_ALIGNMENTlocaleLOCKmaxSizemaxSizeSetminSizeminSizeSetmouseListenermouseListenerKmouseMotionListenermouseMotionListenerKmouseWheelListenermouseWheelListenerKnewEventsOnlyownedWindowKparentpeerpeerFontpopupsprefSizeprefSizeSetRIGHT_ALIGNMENTtextListenerKTOP_ALIGNMENTvisiblewidthwindowFocusListenerKwindowListenerKwindowStateListenerKxy

Constructor Summary

AccessConstructor and Description
public
Scrollbar()

Constructs a new vertical scroll bar.

public
Scrollbar(int
indicates the orientation of the scroll bar
orientation
)

Constructs a new scroll bar with the specified orientation.

public
Scrollbar(int
indicates the orientation of the scroll bar.
orientation
,
int
the initial value of the scroll bar
value
,
int
the visible amount of the scroll bar, typically represented by the size of the bubble
visible
,
int
the minimum value of the scroll bar
minimum
,
int
the maximum value of the scroll bar
maximum
)

Constructs a new scroll bar with the specified orientation, initial value, visible amount, and minimum and maximum values.

Method Summary

Modifier and TypeMethod and Description
public synchronized void
addAdjustmentListener(AdjustmentListener
the adjustment listener
l
)

Implements java.awt.Adjustable.addAdjustmentListener.

Adds the specified adjustment listener to receive instances of AdjustmentEvent from this scroll bar.

public void
addNotify()

Overrides java.awt.Component.addNotify.

Creates the Scrollbar's peer.

pack-priv String
constructComponentName()

Overrides java.awt.Component.constructComponentName.

Constructs a name for this component.

pack-priv boolean
public AccessibleContext

Returns:

an AccessibleAWTScrollBar that serves as the AccessibleContext of this ScrollBar
getAccessibleContext
()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this Scrollbar.

public synchronized AdjustmentListener[]

Returns:

all of this scrollbar's AdjustmentListeners or an empty array if no adjustment listeners are currently registered
getAdjustmentListeners
()

Returns an array of all the adjustment listeners registered on this scrollbar.

public int

Returns:

the block increment of this scroll bar
getBlockIncrement
()

Implements java.awt.Adjustable.getBlockIncrement.

Gets the block increment of this scroll bar.

public int

Returns:

the unit increment for this scrollbar
getLineIncrement
()
Deprecated As of JDK version 1.1, replaced by getUnitIncrement().

Returns the unit increment for this scrollbar.

public <T extends EventListener> T[]

Returns:

an array of all objects registered as FooListeners on this component, or an empty array if no such listeners have been added
getListeners
(Class<T>
the type of listeners requested; this parameter should specify an interface that descends from java.util.EventListener
listenerType
)

Overrides java.awt.Component.getListeners.

Returns an array of all the objects currently registered as FooListeners upon this Scrollbar.

public int

Returns:

the maximum value of this scroll bar
getMaximum
()

Implements java.awt.Adjustable.getMaximum.

Gets the maximum value of this scroll bar.

public int

Returns:

the minimum value of this scroll bar
getMinimum
()

Implements java.awt.Adjustable.getMinimum.

Gets the minimum value of this scroll bar.

public int

Returns:

the orientation of this scroll bar, either Scrollbar.HORIZONTAL or Scrollbar.VERTICAL
getOrientation
()

Implements java.awt.Adjustable.getOrientation.

Returns the orientation of this scroll bar.

public int

Returns:

the block increment of this scroll bar
getPageIncrement
()
Deprecated As of JDK version 1.1, replaced by getBlockIncrement().

Returns the block increment of this scroll bar.

public int

Returns:

the unit increment of this scroll bar
getUnitIncrement
()

Implements java.awt.Adjustable.getUnitIncrement.

Gets the unit increment for this scrollbar.

public int

Returns:

the current value of this scroll bar
getValue
()

Implements java.awt.Adjustable.getValue.

Gets the current value of this scroll bar.

public boolean

Returns:

the value of the valueIsAdjusting property
getValueIsAdjusting
()

Returns true if the value is in the process of changing as a result of actions being taken by the user.

public int

Returns:

the visible amount of this scroll bar
getVisible
()
Deprecated As of JDK version 1.1, replaced by getVisibleAmount().

Returns the visible amount of this scroll bar.

public int

Returns:

the visible amount of this scroll bar
getVisibleAmount
()

Implements java.awt.Adjustable.getVisibleAmount.

Gets the visible amount of this scroll bar.

private static native void
initIDs()

Hides java.awt.Component.initIDs.

Initialize JNI field and method IDs.

protected String

Returns:

the parameter string of this scroll bar
paramString
()

Overrides java.awt.Component.paramString.

Returns a string representing the state of this Scrollbar.

protected void
processAdjustmentEvent(AdjustmentEvent
the adjustment event
e
)

Processes adjustment events occurring on this scrollbar by dispatching them to any registered AdjustmentListener objects.

protected void
processEvent(AWTEvent
the event
e
)

Overrides java.awt.Component.processEvent.

Processes events on this scroll bar.

private void
readObject(ObjectInputStream
the ObjectInputStream to read
s
)

Hides java.awt.Component.readObject.

Reads the ObjectInputStream and if it isn't null adds a listener to receive adjustment events fired by the Scrollbar.

public synchronized void
removeAdjustmentListener(AdjustmentListener
the adjustment listener
l
)

Implements java.awt.Adjustable.removeAdjustmentListener.

Removes the specified adjustment listener so that it no longer receives instances of AdjustmentEvent from this scroll bar.

public void
setBlockIncrement(int
the amount by which to increment or decrement the scroll bar's value
v
)

Implements java.awt.Adjustable.setBlockIncrement.

Sets the block increment for this scroll bar.

public synchronized void
setLineIncrement(int
the increment value
v
)
Deprecated As of JDK version 1.1, replaced by setUnitIncrement(int).

Sets the unit increment for this scroll bar.

public void
setMaximum(int
the new maximum value for this scroll bar
newMaximum
)

Implements java.awt.Adjustable.setMaximum.

Sets the maximum value of this scroll bar.

public void
setMinimum(int
the new minimum value for this scroll bar
newMinimum
)

Implements java.awt.Adjustable.setMinimum.

Sets the minimum value of this scroll bar.

public void
setOrientation(int
the orientation of this scroll bar, either Scrollbar.HORIZONTAL or Scrollbar.VERTICAL
orientation
)

Sets the orientation for this scroll bar.

public synchronized void
setPageIncrement(int
the block increment
v
)
Deprecated As of JDK version 1.1, replaced by setBlockIncrement().

Sets the block increment for this scroll bar.

public void
setUnitIncrement(int
the amount by which to increment or decrement the scroll bar's value
v
)

Implements java.awt.Adjustable.setUnitIncrement.

Sets the unit increment for this scroll bar.

public void
setValue(int
the new value of the scroll bar
newValue
)

Implements java.awt.Adjustable.setValue.

Sets the value of this scroll bar to the specified value.

public void
setValueIsAdjusting(boolean
new adjustment-in-progress status
b
)

Sets the valueIsAdjusting property.

public void
setValues(int
is the position in the current window
value
,
int
is the visible amount of the scroll bar
visible
,
int
is the minimum value of the scroll bar
minimum
,
int
is the maximum value of the scroll bar
maximum
)

Sets the values of four properties for this scroll bar: value, visibleAmount, minimum, and maximum.

public void
setVisibleAmount(int
the new visible amount
newAmount
)

Implements java.awt.Adjustable.setVisibleAmount.

Sets the visible amount of this scroll bar.

private void
writeObject(ObjectOutputStream
the ObjectOutputStream to write
s
)

Hides java.awt.Component.writeObject.

Writes default serializable fields to stream.

Inherited from java.awt.Component:
actionaddaddComponentListeneraddFocusListeneraddHierarchyBoundsListeneraddHierarchyListeneraddInputMethodListeneraddKeyListeneraddMouseListeneraddMouseMotionListeneraddMouseWheelListeneraddPropertyChangeListeneraddPropertyChangeListeneradjustListeningChildrenOnParentapplyComponentOrientationapplyCompoundShapeapplyCurrentShapeareBoundsValidareFocusTraversalKeysSetareInputMethodsEnabledautoProcessMouseWheelboundscanBeFocusOwnercanBeFocusOwnerRecursivelycheckGDcheckImagecheckImagecheckTreeLockclearCurrentFocusCycleRootOnHideclearMostRecentFocusOwnerOnHidecoalesceEventscontainscontainscontainsFocuscountHierarchyMemberscreateBufferStrategycreateBufferStrategycreateHierarchyEventscreateImagecreateImagecreateVolatileImagecreateVolatileImagedeliverEventdisabledisableEventsdispatchEventdispatchEventImpldispatchMouseWheelToAncestordoLayoutenableenableenableEventsenableInputMethodseventTypeEnabledfindUnderMouseInWindowfirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangegetAccessibleIndexInParentgetAccessibleStateSetgetAlignmentXgetAlignmentYgetBackBuffergetBackgroundgetBaselinegetBaselineResizeBehaviorgetBoundsgetBoundsgetBoundsOpgetBufferStrategygetColorModelgetComponentAtgetComponentAtgetComponentFactorygetComponentListenersgetComponentOrientationgetContainergetContainingWindowgetCursorgetCursor_NoClientCodegetDropTargetgetFocusCycleRootAncestorgetFocusListenersgetFocusTraversalKeysgetFocusTraversalKeys_NoIDCheckgetFocusTraversalKeysEnabledgetFontgetFont_NoClientCodegetFontMetricsgetForegroundgetGraphicsgetGraphics_NoClientCodegetGraphicsConfigurationgetGraphicsConfiguration_NoClientCodegetHeightgetHierarchyBoundsListenersgetHierarchyListenersgetHWPeerAboveMegetIgnoreRepaintgetInputContextgetInputMethodListenersgetInputMethodRequestsgetKeyListenersgetLocalegetLocationgetLocationgetLocationOnScreengetLocationOnScreen_NoTreeLockgetLocationOnWindowgetMaximumSizegetMinimumSizegetMouseListenersgetMouseMotionListenersgetMousePositiongetMouseWheelListenersgetNamegetNativeContainergetNextFocusCandidategetNormalShapegetObjectLockgetOpaqueShapegetParentgetParent_NoClientCodegetPreferredSizegetPropertyChangeListenersgetPropertyChangeListenersgetSiblingIndexAbovegetSiblingIndexBelowgetSizegetSizegetToolkitgetToolkitImplgetTraversalRootgetTreeLockgetWidthgetXgetYgotFocushandleEventhasFocushideimageUpdateinitializeFocusTraversalKeysinsideinvalidateinvalidateIfValidinvalidateParentisAutoFocusTransferOnDisposalisBackgroundSetisCoalescingEnabledisCursorSetisDisplayableisDoubleBufferedisEnabledisEnabledImplisFocusableisFocusCycleRootisFocusOwnerisFocusTraversableisFocusTraversableOverriddenisFontSetisForegroundSetisInstanceOfisLightweightisMaximumSizeSetisMinimumSizeSetisMixingNeededisNonOpaqueForMixingisOpaqueisPreferredSizeSetisRecursivelyVisibleisSameOrAncestorOfisShowingisValidisVisibleisVisible_NoClientCodekeyDownkeyUplayoutlightweightPaintlightweightPrintlistlistlistlistlistlocatelocationlostFocusminimumSizemixOnHidingmixOnReshapingmixOnShowingmixOnValidatingmixOnZOrderChangingmouseDownmouseDragmouseEntermouseExitmouseMovemouseUpmovenextFocusnumListeningpaintpaintAllpaintHeavyweightComponentspointRelativeToComponentpostEventpostsOldMouseEventspreferredSizeprepareImageprepareImageprintprintAllprintHeavyweightComponentsprocessComponentEventprocessFocusEventprocessHierarchyBoundsEventprocessHierarchyEventprocessInputMethodEventprocessKeyEventprocessMouseEventprocessMouseMotionEventprocessMouseWheelEventrelocateComponentremoveremoveComponentListenerremoveFocusListenerremoveHierarchyBoundsListenerremoveHierarchyListenerremoveInputMethodListenerremoveKeyListenerremoveMouseListenerremoveMouseMotionListenerremoveMouseWheelListenerremoveNotifyremovePropertyChangeListenerremovePropertyChangeListenerrepaintrepaintrepaintrepaintrequestFocusrequestFocusrequestFocusrequestFocusrequestFocusHelperrequestFocusHelperrequestFocusInWindowrequestFocusInWindowrequestFocusInWindowrequestFocusInWindowreshaperesizeresizerevalidaterevalidateSynchronouslysetAutoFocusTransferOnDisposalsetBackgroundsetBoundssetBoundssetBoundsOpsetComponentOrientationsetCursorsetDropTargetsetEnabledsetFocusablesetFocusTraversalKeyssetFocusTraversalKeys_NoIDChecksetFocusTraversalKeysEnabledsetFontsetForegroundsetGraphicsConfigurationsetIgnoreRepaintsetLocalesetLocationsetLocationsetMaximumSizesetMinimumSizesetMixingCutoutShapesetNamesetPreferredSizesetRequestFocusControllersetSizesetSizesetVisibleshowshowsizesubtractAndApplyShapesubtractAndApplyShapeBelowMetoStringtransferFocustransferFocustransferFocusBackwardtransferFocusBackwardtransferFocusUpCycleupdateupdateChildGraphicsDataupdateCursorImmediatelyupdateGraphicsDataupdateZOrdervalidate