public class Scrollbar extends Component implements Adjustable, Accessible
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:
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:
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:
AdjustmentEvent.TRACK
is sent out when the
user drags the scroll bar's bubble.
AdjustmentEvent.UNIT_INCREMENT
is sent out
when the user clicks in the left arrow of a horizontal scroll
bar, or the top arrow of a vertical scroll bar, or makes the
equivalent gesture from the keyboard.
AdjustmentEvent.UNIT_DECREMENT
is sent out
when the user clicks in the right arrow of a horizontal scroll
bar, or the bottom arrow of a vertical scroll bar, or makes the
equivalent gesture from the keyboard.
AdjustmentEvent.BLOCK_INCREMENT
is sent out
when the user clicks in the track, to the left of the bubble
on a horizontal scroll bar, or above the bubble on a vertical
scroll bar. By convention, the Page Up
key is equivalent, if the user is using a keyboard that
defines a Page Up key.
AdjustmentEvent.BLOCK_DECREMENT
is sent out
when the user clicks in the track, to the right of the bubble
on a horizontal scroll bar, or below the bubble on a vertical
scroll bar. By convention, the Page Down
key is equivalent, if the user is using a keyboard that
defines a Page Down key.
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.
AdjustmentEvent.TRACK
replaces
Event.SCROLL_ABSOLUTE
AdjustmentEvent.UNIT_INCREMENT
replaces
Event.SCROLL_LINE_UP
AdjustmentEvent.UNIT_DECREMENT
replaces
Event.SCROLL_LINE_DOWN
AdjustmentEvent.BLOCK_INCREMENT
replaces
Event.SCROLL_PAGE_UP
AdjustmentEvent.BLOCK_DECREMENT
replaces
Event.SCROLL_PAGE_DOWN
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.
AdjustmentEvent
,
AdjustmentListener
,
Serialized FormModifier and Type | Class and Description |
---|---|
protected class |
Scrollbar.AccessibleAWTScrollBar
This class implements accessibility support for the
Scrollbar class. |
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
Modifier and Type | Field and Description |
---|---|
static int |
HORIZONTAL
A constant that indicates a horizontal scroll bar.
|
static int |
VERTICAL
A constant that indicates a vertical scroll bar.
|
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
NO_ORIENTATION
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Constructor and Description |
---|
Scrollbar()
Constructs a new vertical scroll bar.
|
Scrollbar(int orientation)
Constructs a new scroll bar with the specified orientation.
|
Scrollbar(int orientation,
int value,
int visible,
int minimum,
int maximum)
Constructs a new scroll bar with the specified orientation,
initial value, visible amount, and minimum and maximum values.
|
Modifier and Type | Method and Description |
---|---|
void |
addAdjustmentListener(AdjustmentListener l)
Adds the specified adjustment listener to receive instances of
AdjustmentEvent from this scroll bar. |
void |
addNotify()
Creates the
Scrollbar 's peer. |
AccessibleContext |
getAccessibleContext()
Gets the
AccessibleContext associated with this
Scrollbar . |
AdjustmentListener[] |
getAdjustmentListeners()
Returns an array of all the adjustment listeners
registered on this scrollbar.
|
int |
getBlockIncrement()
Gets the block increment of this scroll bar.
|
int |
getLineIncrement()
Deprecated.
As of JDK version 1.1,
replaced by
getUnitIncrement() . |
<T extends EventListener> |
getListeners(Class<T> listenerType)
Returns an array of all the objects currently registered
as
FooListener s
upon this Scrollbar . |
int |
getMaximum()
Gets the maximum value of this scroll bar.
|
int |
getMinimum()
Gets the minimum value of this scroll bar.
|
int |
getOrientation()
Returns the orientation of this scroll bar.
|
int |
getPageIncrement()
Deprecated.
As of JDK version 1.1,
replaced by
getBlockIncrement() . |
int |
getUnitIncrement()
Gets the unit increment for this scrollbar.
|
int |
getValue()
Gets the current value of this scroll bar.
|
boolean |
getValueIsAdjusting()
Returns true if the value is in the process of changing as a
result of actions being taken by the user.
|
int |
getVisible()
Deprecated.
As of JDK version 1.1,
replaced by
getVisibleAmount() . |
int |
getVisibleAmount()
Gets the visible amount of this scroll bar.
|
protected String |
paramString()
Returns a string representing the state of this
Scrollbar . |
protected void |
processAdjustmentEvent(AdjustmentEvent e)
Processes adjustment events occurring on this
scrollbar by dispatching them to any registered
AdjustmentListener objects. |
protected void |
processEvent(AWTEvent e)
Processes events on this scroll bar.
|
void |
removeAdjustmentListener(AdjustmentListener l)
Removes the specified adjustment listener so that it no longer
receives instances of
AdjustmentEvent from this scroll bar. |
void |
setBlockIncrement(int v)
Sets the block increment for this scroll bar.
|
void |
setLineIncrement(int v)
Deprecated.
As of JDK version 1.1,
replaced by
setUnitIncrement(int) . |
void |
setMaximum(int newMaximum)
Sets the maximum value of this scroll bar.
|
void |
setMinimum(int newMinimum)
Sets the minimum value of this scroll bar.
|
void |
setOrientation(int orientation)
Sets the orientation for this scroll bar.
|
void |
setPageIncrement(int v)
Deprecated.
As of JDK version 1.1,
replaced by
setBlockIncrement() . |
void |
setUnitIncrement(int v)
Sets the unit increment for this scroll bar.
|
void |
setValue(int newValue)
Sets the value of this scroll bar to the specified value.
|
void |
setValueIsAdjusting(boolean b)
Sets the
valueIsAdjusting property. |
void |
setValues(int value,
int visible,
int minimum,
int maximum)
Sets the values of four properties for this scroll bar:
value , visibleAmount ,
minimum , and maximum . |
void |
setVisibleAmount(int newAmount)
Sets the visible amount of this scroll bar.
|
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener,