FileDocCategorySizeDatePackage
JScrollBar.javaAPI DocJava SE 6 API27920Tue Jun 10 00:26:38 BST 2008javax.swing

JScrollBar

public class JScrollBar extends JComponent implements Adjustable, Accessible
An implementation of a scrollbar. The user positions the knob in the scrollbar to determine the contents of the viewing area. The program typically adjusts the display so that the end of the scrollbar represents the end of the displayable contents, or 100% of the contents. The start of the scrollbar is the beginning of the displayable contents, or 0%. The position of the knob within those bounds then translates to the corresponding percentage of the displayable contents.

Typically, as the position of the knob in the scrollbar changes a corresponding change is made to the position of the JViewport on the underlying view, changing the contents of the JViewport.

Warning: Swing is not thread safe. For more information see Swing's Threading Policy.

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 {@link java.beans.XMLEncoder}.

see
JScrollPane
beaninfo
attribute: isContainer false description: A component that helps determine the visible content range of an area.
version
1.81 08/08/06
author
David Kloba

Fields Summary
private static final String
uiClassID
private ChangeListener
fwdAdjustmentEvents
All changes from the model are treated as though the user moved the scrollbar knob.
protected BoundedRangeModel
model
The model that represents the scrollbar's minimum, maximum, extent (aka "visibleAmount") and current value.
protected int
orientation
protected int
unitIncrement
protected int
blockIncrement
Constructors Summary
public JScrollBar(int orientation, int value, int extent, int min, int max)
Creates a scrollbar with the specified orientation, value, extent, minimum, and maximum. The "extent" is the size of the viewable area. It is also known as the "visible amount".

Note: Use setBlockIncrement to set the block increment to a size slightly smaller than the view's extent. That way, when the user jumps the knob to an adjacent position, one or two lines of the original contents remain in view.

exception
IllegalArgumentException if orientation is not one of VERTICAL, HORIZONTAL
see
#setOrientation
see
#setValue
see
#setVisibleAmount
see
#setMinimum
see
#setMaximum

        checkOrientation(orientation);
        this.unitIncrement = 1;
        this.blockIncrement = (extent == 0) ? 1 : extent;
        this.orientation = orientation;
        this.model = new DefaultBoundedRangeModel(value, extent, min, max);
        this.model.addChangeListener(fwdAdjustmentEvents);
	setRequestFocusEnabled(false);
        updateUI();
    
public JScrollBar(int orientation)
Creates a scrollbar with the specified orientation and the following initial values:
minimum = 0
maximum = 100
value = 0
extent = 10

        this(orientation, 0, 10, 0, 100);
    
public JScrollBar()
Creates a vertical scrollbar with the following initial values:
minimum = 0
maximum = 100
value = 0
extent = 10

        this(VERTICAL);
    
Methods Summary
public voidaddAdjustmentListener(java.awt.event.AdjustmentListener l)
Adds an AdjustmentListener. Adjustment listeners are notified each time the scrollbar's model changes. Adjustment events are provided for backwards compatibility with java.awt.Scrollbar.

Note that the AdjustmentEvents type property will always have a placeholder value of AdjustmentEvent.TRACK because all changes to a BoundedRangeModels value are considered equivalent. To change the value of a BoundedRangeModel one just sets its value property, i.e. model.setValue(123). No information about the origin of the change, e.g. it's a block decrement, is provided. We don't try fabricate the origin of the change here.

param
l the AdjustmentLister to add
see
#removeAdjustmentListener
see
BoundedRangeModel#addChangeListener

        listenerList.add(AdjustmentListener.class, l);
    
private voidcheckOrientation(int orientation)


    
        
        switch (orientation) {
        case VERTICAL:
        case HORIZONTAL:
            break;
        default:
            throw new IllegalArgumentException("orientation must be one of: VERTICAL, HORIZONTAL");
        }
    
protected voidfireAdjustmentValueChanged(int id, int type, int value)
Notify listeners that the scrollbar's model has changed.

see
#addAdjustmentListener
see
EventListenerList

	fireAdjustmentValueChanged(id, type, value, getValueIsAdjusting());
    
private voidfireAdjustmentValueChanged(int id, int type, int value, boolean isAdjusting)
Notify listeners that the scrollbar's model has changed.

see
#addAdjustmentListener
see
EventListenerList

        Object[] listeners = listenerList.getListenerList();
        AdjustmentEvent e = null;
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
            if (listeners[i]==AdjustmentListener.class) {
                if (e == null) {
                    e = new AdjustmentEvent(this, id, type, value, isAdjusting);
                }
                ((AdjustmentListener)listeners[i+1]).adjustmentValueChanged(e);
            }          
        }
    
public javax.accessibility.AccessibleContextgetAccessibleContext()
Gets the AccessibleContext associated with this JScrollBar. For JScrollBar, the AccessibleContext takes the form of an AccessibleJScrollBar. A new AccessibleJScrollBar instance is created if necessary.

return
an AccessibleJScrollBar that serves as the AccessibleContext of this JScrollBar

        if (accessibleContext == null) {
            accessibleContext = new AccessibleJScrollBar();
        }
        return accessibleContext;
    
public java.awt.event.AdjustmentListener[]getAdjustmentListeners()
Returns an array of all the AdjustmentListeners added to this JScrollBar with addAdjustmentListener().

return
all of the AdjustmentListeners added or an empty array if no listeners have been added
since
1.4

        return (AdjustmentListener[])listenerList.getListeners(
                AdjustmentListener.class);
    
public intgetBlockIncrement(int direction)
Returns the amount to change the scrollbar's value by, given a block (usually "page") up/down request. A ScrollBarUI implementation typically calls this method when the user clicks above or below the scrollbar "knob" to change the value up or down by large amount. Subclasses my override this method to compute a value, e.g. the change required to scroll up or down one paragraph in a text document.

The JScrollPane component creates scrollbars (by default) that override this method and delegate to the viewports Scrollable view, if it has one. The Scrollable interface provides a more specialized version of this method.

param
direction is -1 or 1 for up/down respectively
return
the value of the blockIncrement property
see
#setBlockIncrement
see
#setValue
see
Scrollable#getScrollableBlockIncrement

 
        return blockIncrement; 
    
public intgetBlockIncrement()
For backwards compatibility with java.awt.Scrollbar.

see
Adjustable#getBlockIncrement
see
#getBlockIncrement(int)

        return blockIncrement;
    
public intgetMaximum()
The maximum value of the scrollbar is maximum - extent.

return
the value of the model's maximum property
see
#setMaximum

 
        return getModel().getMaximum(); 
    
public java.awt.DimensiongetMaximumSize()
The scrollbar is flexible along it's scrolling axis and rigid along the other axis.

        Dimension pref = getPreferredSize();
        if (getOrientation() == VERTICAL) {
            return new Dimension(pref.width, Short.MAX_VALUE);
        } else {
            return new Dimension(Short.MAX_VALUE, pref.height);
        }
    
public intgetMinimum()
Returns the minimum value supported by the scrollbar (usually zero).

return
the value of the model's minimum property
see
#setMinimum

 
        return getModel().getMinimum(); 
    
public java.awt.DimensiongetMinimumSize()
The scrollbar is flexible along it's scrolling axis and rigid along the other axis.

        Dimension pref = getPreferredSize();
        if (orientation == VERTICAL) {
            return new Dimension(pref.width, 5);
        } else {
            return new Dimension(5, pref.height);
        }
    
public javax.swing.BoundedRangeModelgetModel()
Returns data model that handles the scrollbar's four fundamental properties: minimum, maximum, value, extent.

see
#setModel

 
        return model; 
    
public intgetOrientation()
Returns the component's orientation (horizontal or vertical).

return
VERTICAL or HORIZONTAL
see
#setOrientation
see
java.awt.Adjustable#getOrientation

 
        return orientation; 
    
public javax.swing.plaf.ScrollBarUIgetUI()
Returns the delegate that implements the look and feel for this component.

see
JComponent#setUI

 
        return (ScrollBarUI)ui;
    
public java.lang.StringgetUIClassID()
Returns the name of the LookAndFeel class for this component.

return
"ScrollBarUI"
see
JComponent#getUIClassID
see
UIDefaults#getUI

        return uiClassID;
    
public intgetUnitIncrement(int direction)
Returns the amount to change the scrollbar's value by, given a unit up/down request. A ScrollBarUI implementation typically calls this method when the user clicks on a scrollbar up/down arrow and uses the result to update the scrollbar's value. Subclasses my override this method to compute a value, e.g. the change required to scroll up or down one (variable height) line text or one row in a table.

The JScrollPane component creates scrollbars (by default) that override this method and delegate to the viewports Scrollable view, if it has one. The Scrollable interface provides a more specialized version of this method.

param
direction is -1 or 1 for up/down respectively
return
the value of the unitIncrement property
see
#setUnitIncrement
see
#setValue
see
Scrollable#getScrollableUnitIncrement

 
        return unitIncrement; 
    
public intgetUnitIncrement()
For backwards compatibility with java.awt.Scrollbar.

see
Adjustable#getUnitIncrement
see
#getUnitIncrement(int)

        return unitIncrement;
    
public intgetValue()
Returns the scrollbar's value.

return
the model's value property
see
#setValue

 
        return getModel().getValue(); 
    
public booleangetValueIsAdjusting()
True if the scrollbar knob is being dragged.

return
the value of the model's valueIsAdjusting property
see
#setValueIsAdjusting

 
        return getModel().getValueIsAdjusting(); 
    
public intgetVisibleAmount()
Returns the scrollbar's extent, aka its "visibleAmount". In many scrollbar look and feel implementations the size of the scrollbar "knob" or "thumb" is proportional to the extent.

return
the value of the model's extent property
see
#setVisibleAmount

 
        return getModel().getExtent(); 
    
protected java.lang.StringparamString()
Returns a string representation of this JScrollBar. 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.

return
a string representation of this JScrollBar.

	String orientationString = (orientation == HORIZONTAL ?
				    "HORIZONTAL" : "VERTICAL");

	return super.paramString() +
	",blockIncrement=" + blockIncrement +
	",orientation=" + orientationString +
	",unitIncrement=" + unitIncrement;
    
public voidremoveAdjustmentListener(java.awt.event.AdjustmentListener l)
Removes an AdjustmentEvent listener.

param
l the AdjustmentLister to remove
see
#addAdjustmentListener

        listenerList.remove(AdjustmentListener.class, l);
    
public voidsetBlockIncrement(int blockIncrement)
Sets the blockIncrement property.

Note, that if the argument is equal to the value of Integer.MIN_VALUE, the most look and feels will not provide the scrolling to the right/down.

see
#getBlockIncrement()
beaninfo
preferred: true bound: true description: The scrollbar's block increment.

 
        int oldValue = this.blockIncrement;
        this.blockIncrement = blockIncrement;
        firePropertyChange("blockIncrement", oldValue, blockIncrement);
    
public voidsetEnabled(boolean x)
Enables the component so that the knob position can be changed. When the disabled, the knob position cannot be changed.

param
x a boolean value, where true enables the component and false disables it

        super.setEnabled(x);
        Component[] children = getComponents();
        for(int i = 0; i < children.length; i++) {
            children[i].setEnabled(x);
        }
    
public voidsetMaximum(int maximum)
Sets the model's maximum property. Note that the scrollbar's value can only be set to maximum - extent.

see
#getMaximum
see
BoundedRangeModel#setMaximum
beaninfo
preferred: true description: The scrollbar's maximum value.

 
        getModel().setMaximum(maximum); 
    
public voidsetMinimum(int minimum)
Sets the model's minimum property.

see
#getMinimum
see
BoundedRangeModel#setMinimum
beaninfo
preferred: true description: The scrollbar's minimum value.

 
        getModel().setMinimum(minimum); 
    
public voidsetModel(javax.swing.BoundedRangeModel newModel)
Sets the model that handles the scrollbar's four fundamental properties: minimum, maximum, value, extent.

see
#getModel
beaninfo
bound: true expert: true description: The scrollbar's BoundedRangeModel.

        Integer oldValue = null;
        BoundedRangeModel oldModel = model;
        if (model != null) {
            model.removeChangeListener(fwdAdjustmentEvents);
            oldValue = new Integer(model.getValue());
        }
        model = newModel;
        if (model != null) {
            model.addChangeListener(fwdAdjustmentEvents);
        }

        firePropertyChange("model", oldModel, model);

        if (accessibleContext != null) {
            accessibleContext.firePropertyChange(
                    AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
                    oldValue, new Integer(model.getValue()));        
        }
    
public voidsetOrientation(int orientation)
Set the scrollbar's orientation to either VERTICAL or HORIZONTAL.

exception
IllegalArgumentException if orientation is not one of VERTICAL, HORIZONTAL
see
#getOrientation
beaninfo
preferred: true bound: true attribute: visualUpdate true description: The scrollbar's orientation. enum: VERTICAL JScrollBar.VERTICAL HORIZONTAL JScrollBar.HORIZONTAL

 
        checkOrientation(orientation);
        int oldValue = this.orientation;
        this.orientation = orientation;
        firePropertyChange("orientation", oldValue, orientation);

        if ((oldValue != orientation) && (accessibleContext != null)) {
            accessibleContext.firePropertyChange(
                    AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
                    ((oldValue == VERTICAL) 
                     ? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL),
                    ((orientation == VERTICAL) 
                     ? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL));
        }
        if (orientation != oldValue) {
            revalidate();
        }
    
public voidsetUI(javax.swing.plaf.ScrollBarUI ui)
Sets the L&F object that renders this component.

param
ui the ScrollBarUI L&F object
see
UIDefaults#getUI
since
1.4
beaninfo
bound: true hidden: true attribute: visualUpdate true description: The UI object that implements the Component's LookAndFeel

        super.setUI(ui);
    
public voidsetUnitIncrement(int unitIncrement)
Sets the unitIncrement property.

Note, that if the argument is equal to the value of Integer.MIN_VALUE, the most look and feels will not provide the scrolling to the right/down.

see
#getUnitIncrement
beaninfo
preferred: true bound: true description: The scrollbar's unit increment.

 
        int oldValue = this.unitIncrement;
        this.unitIncrement = unitIncrement;
        firePropertyChange("unitIncrement", oldValue, unitIncrement);
    
public voidsetValue(int value)
Sets the scrollbar's value. This method just forwards the value to the model.

see
#getValue
see
BoundedRangeModel#setValue
beaninfo
preferred: true description: The scrollbar's current value.

        BoundedRangeModel m = getModel();
        int oldValue = m.getValue();
        m.setValue(value);

        if (accessibleContext != null) {
            accessibleContext.firePropertyChange(
                    AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
                    new Integer(oldValue),
                    new Integer(m.getValue()));
        }
    
public voidsetValueIsAdjusting(boolean b)
Sets the model's valueIsAdjusting property. Scrollbar look and feel implementations should set this property to true when a knob drag begins, and to false when the drag ends. The scrollbar model will not generate ChangeEvents while valueIsAdjusting is true.

see
#getValueIsAdjusting
see
BoundedRangeModel#setValueIsAdjusting
beaninfo
expert: true description: True if the scrollbar thumb is being dragged.

 
        BoundedRangeModel m = getModel();   
        boolean oldValue = m.getValueIsAdjusting();
        m.setValueIsAdjusting(b);
   
        if ((oldValue != b) && (accessibleContext != null)) {
            accessibleContext.firePropertyChange(
                    AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
                    ((oldValue) ? AccessibleState.BUSY : null),
                    ((b) ? AccessibleState.BUSY : null));
        }
    
public voidsetValues(int newValue, int newExtent, int newMin, int newMax)
Sets the four BoundedRangeModel properties after forcing the arguments to obey the usual constraints:
minimum <= value <= value+extent <= maximum

see
BoundedRangeModel#setRangeProperties
see
#setValue
see
#setVisibleAmount
see
#setMinimum
see
#setMaximum

        BoundedRangeModel m = getModel();
        int oldValue = m.getValue();
        m.setRangeProperties(newValue, newExtent, newMin, newMax, m.getValueIsAdjusting());

        if (accessibleContext != null) {
            accessibleContext.firePropertyChange(
                    AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
                    new Integer(oldValue),
                    new Integer(m.getValue()));
        }
    
public voidsetVisibleAmount(int extent)
Set the model's extent property.

see
#getVisibleAmount
see
BoundedRangeModel#setExtent
beaninfo
preferred: true description: The amount of the view that is currently visible.

 
        getModel().setExtent(extent); 
    
public voidupdateUI()
Overrides JComponent.updateUI.

see
JComponent#updateUI

        setUI((ScrollBarUI)UIManager.getUI(this));
    
private voidwriteObject(java.io.ObjectOutputStream s)
See readObject() and writeObject() in JComponent for more information about serialization in Swing.

        s.defaultWriteObject();
        if (getUIClassID().equals(uiClassID)) {
            byte count = JComponent.getWriteObjCounter(this);
            JComponent.setWriteObjCounter(this, --count);
            if (count == 0 && ui != null) {
                ui.installUI(this);
            }
        }