< prev index next >

src/java.desktop/share/classes/javax/swing/JScrollBar.java

Print this page




  51  * of the contents. The start of the scrollbar is the beginning of the
  52  * displayable contents, or 0%. The position of the knob within
  53  * those bounds then translates to the corresponding percentage of
  54  * the displayable contents.
  55  * <p>
  56  * Typically, as the position of the knob in the scrollbar changes
  57  * a corresponding change is made to the position of the JViewport on
  58  * the underlying view, changing the contents of the JViewport.
  59  * <p>
  60  * <strong>Warning:</strong> Swing is not thread safe. For more
  61  * information see <a
  62  * href="package-summary.html#threading">Swing's Threading
  63  * Policy</a>.
  64  * <p>
  65  * <strong>Warning:</strong>
  66  * Serialized objects of this class will not be compatible with
  67  * future Swing releases. The current serialization support is
  68  * appropriate for short term storage or RMI between applications running
  69  * the same version of Swing.  As of 1.4, support for long term storage
  70  * of all JavaBeans&trade;
  71  * has been added to the <code>java.beans</code> package.
  72  * Please see {@link java.beans.XMLEncoder}.
  73  *
  74  * @see JScrollPane
  75  * @beaninfo
  76  *      attribute: isContainer false
  77  *    description: A component that helps determine the visible content range of an area.
  78  *
  79  * @author David Kloba
  80  * @since 1.2
  81  */
  82 @SuppressWarnings("serial") // Same-version serialization only
  83 public class JScrollBar extends JComponent implements Adjustable, Accessible
  84 {
  85     /**
  86      * @see #getUIClassID
  87      * @see #readObject
  88      */
  89     private static final String uiClassID = "ScrollBarUI";
  90 
  91     /**


 121     protected int blockIncrement;
 122 
 123 
 124     private void checkOrientation(int orientation) {
 125         switch (orientation) {
 126         case VERTICAL:
 127         case HORIZONTAL:
 128             break;
 129         default:
 130             throw new IllegalArgumentException("orientation must be one of: VERTICAL, HORIZONTAL");
 131         }
 132     }
 133 
 134 
 135     /**
 136      * Creates a scrollbar with the specified orientation,
 137      * value, extent, minimum, and maximum.
 138      * The "extent" is the size of the viewable area. It is also known
 139      * as the "visible amount".
 140      * <p>
 141      * Note: Use <code>setBlockIncrement</code> to set the block
 142      * increment to a size slightly smaller than the view's extent.
 143      * That way, when the user jumps the knob to an adjacent position,
 144      * one or two lines of the original contents remain in view.
 145      *
 146      * @exception IllegalArgumentException if orientation is not one of VERTICAL, HORIZONTAL
 147      *
 148      * @see #setOrientation
 149      * @see #setValue
 150      * @see #setVisibleAmount
 151      * @see #setMinimum
 152      * @see #setMaximum
 153      *
 154      * @param orientation an orientation of the {@code JScrollBar}
 155      * @param value an int giving the current value
 156      * @param extent an int giving the amount by which the value can "jump"
 157      * @param min an int giving the minimum value
 158      * @param max an int giving the maximum value
 159      */
 160     public JScrollBar(int orientation, int value, int extent, int min, int max)
 161     {


 187     }
 188 
 189 
 190     /**
 191      * Creates a vertical scrollbar with the following initial values:
 192      * <pre>
 193      * minimum = 0
 194      * maximum = 100
 195      * value = 0
 196      * extent = 10
 197      * </pre>
 198      */
 199     public JScrollBar() {
 200         this(VERTICAL);
 201     }
 202 
 203 
 204     /**
 205      * Sets the {@literal L&F} object that renders this component.
 206      *
 207      * @param ui  the <code>ScrollBarUI</code> {@literal L&F} object
 208      * @see UIDefaults#getUI
 209      * @since 1.4
 210      * @beaninfo
 211      *        bound: true
 212      *       hidden: true
 213      *    attribute: visualUpdate true
 214      *  description: The UI object that implements the Component's LookAndFeel
 215      */
 216     public void setUI(ScrollBarUI ui) {
 217         super.setUI(ui);
 218     }
 219 
 220 
 221     /**
 222      * Returns the delegate that implements the look and feel for
 223      * this component.
 224      *
 225      * @return the scroll bar's current UI.
 226      * @see JComponent#setUI
 227      */
 228     public ScrollBarUI getUI() {
 229         return (ScrollBarUI)ui;
 230     }
 231 
 232 
 233     /**
 234      * Overrides <code>JComponent.updateUI</code>.
 235      * @see JComponent#updateUI
 236      */
 237     public void updateUI() {
 238         setUI((ScrollBarUI)UIManager.getUI(this));
 239     }
 240 
 241 
 242     /**
 243      * Returns the name of the LookAndFeel class for this component.
 244      *
 245      * @return "ScrollBarUI"
 246      * @see JComponent#getUIClassID
 247      * @see UIDefaults#getUI
 248      */
 249     public String getUIClassID() {
 250         return uiClassID;
 251     }
 252 
 253 
 254 


 669      * @see #removeAdjustmentListener
 670      * @see BoundedRangeModel#addChangeListener
 671      */
 672     public void addAdjustmentListener(AdjustmentListener l)   {
 673         listenerList.add(AdjustmentListener.class, l);
 674     }
 675 
 676 
 677     /**
 678      * Removes an AdjustmentEvent listener.
 679      *
 680      * @param l the AdjustmentLister to remove
 681      * @see #addAdjustmentListener
 682      */
 683     public void removeAdjustmentListener(AdjustmentListener l)  {
 684         listenerList.remove(AdjustmentListener.class, l);
 685     }
 686 
 687 
 688     /**
 689      * Returns an array of all the <code>AdjustmentListener</code>s added
 690      * to this JScrollBar with addAdjustmentListener().
 691      *
 692      * @return all of the <code>AdjustmentListener</code>s added or an empty
 693      *         array if no listeners have been added
 694      * @since 1.4
 695      */
 696     public AdjustmentListener[] getAdjustmentListeners() {
 697         return listenerList.getListeners(AdjustmentListener.class);
 698     }
 699 
 700 
 701     /**
 702      * Notify listeners that the scrollbar's model has changed.
 703      *
 704      * @param id an integer indicating the type of event.
 705      * @param type an integer indicating the adjustment type.
 706      * @param value the current value of the adjustment
 707      *
 708      * @see #addAdjustmentListener
 709      * @see EventListenerList
 710      */
 711     protected void fireAdjustmentValueChanged(int id, int type, int value) {
 712         fireAdjustmentValueChanged(id, type, value, getValueIsAdjusting());


 801      * See readObject() and writeObject() in JComponent for more
 802      * information about serialization in Swing.
 803      */
 804     private void writeObject(ObjectOutputStream s) throws IOException {
 805         s.defaultWriteObject();
 806         if (getUIClassID().equals(uiClassID)) {
 807             byte count = JComponent.getWriteObjCounter(this);
 808             JComponent.setWriteObjCounter(this, --count);
 809             if (count == 0 && ui != null) {
 810                 ui.installUI(this);
 811             }
 812         }
 813     }
 814 
 815 
 816     /**
 817      * Returns a string representation of this JScrollBar. This method
 818      * is intended to be used only for debugging purposes, and the
 819      * content and format of the returned string may vary between
 820      * implementations. The returned string may be empty but may not
 821      * be <code>null</code>.
 822      *
 823      * @return  a string representation of this JScrollBar.
 824      */
 825     protected String paramString() {
 826         String orientationString = (orientation == HORIZONTAL ?
 827                                     "HORIZONTAL" : "VERTICAL");
 828 
 829         return super.paramString() +
 830         ",blockIncrement=" + blockIncrement +
 831         ",orientation=" + orientationString +
 832         ",unitIncrement=" + unitIncrement;
 833     }
 834 
 835 /////////////////
 836 // Accessibility support
 837 ////////////////
 838 
 839     /**
 840      * Gets the AccessibleContext associated with this JScrollBar.
 841      * For JScrollBar, the AccessibleContext takes the form of an
 842      * AccessibleJScrollBar.
 843      * A new AccessibleJScrollBar instance is created if necessary.
 844      *
 845      * @return an AccessibleJScrollBar that serves as the
 846      *         AccessibleContext of this JScrollBar
 847      */
 848     public AccessibleContext getAccessibleContext() {
 849         if (accessibleContext == null) {
 850             accessibleContext = new AccessibleJScrollBar();
 851         }
 852         return accessibleContext;
 853     }
 854 
 855     /**
 856      * This class implements accessibility support for the
 857      * <code>JScrollBar</code> class.  It provides an implementation of the
 858      * Java Accessibility API appropriate to scroll bar user-interface
 859      * elements.
 860      * <p>
 861      * <strong>Warning:</strong>
 862      * Serialized objects of this class will not be compatible with
 863      * future Swing releases. The current serialization support is
 864      * appropriate for short term storage or RMI between applications running
 865      * the same version of Swing.  As of 1.4, support for long term storage
 866      * of all JavaBeans&trade;
 867      * has been added to the <code>java.beans</code> package.
 868      * Please see {@link java.beans.XMLEncoder}.
 869      */
 870     @SuppressWarnings("serial") // Same-version serialization only
 871     protected class AccessibleJScrollBar extends AccessibleJComponent
 872         implements AccessibleValue {
 873 
 874         /**
 875          * Get the state set of this object.
 876          *
 877          * @return an instance of AccessibleState containing the current state
 878          * of the object
 879          * @see AccessibleState
 880          */
 881         public AccessibleStateSet getAccessibleStateSet() {
 882             AccessibleStateSet states = super.getAccessibleStateSet();
 883             if (getValueIsAdjusting()) {
 884                 states.add(AccessibleState.BUSY);
 885             }
 886             if (getOrientation() == VERTICAL) {
 887                 states.add(AccessibleState.VERTICAL);




  51  * of the contents. The start of the scrollbar is the beginning of the
  52  * displayable contents, or 0%. The position of the knob within
  53  * those bounds then translates to the corresponding percentage of
  54  * the displayable contents.
  55  * <p>
  56  * Typically, as the position of the knob in the scrollbar changes
  57  * a corresponding change is made to the position of the JViewport on
  58  * the underlying view, changing the contents of the JViewport.
  59  * <p>
  60  * <strong>Warning:</strong> Swing is not thread safe. For more
  61  * information see <a
  62  * href="package-summary.html#threading">Swing's Threading
  63  * Policy</a>.
  64  * <p>
  65  * <strong>Warning:</strong>
  66  * Serialized objects of this class will not be compatible with
  67  * future Swing releases. The current serialization support is
  68  * appropriate for short term storage or RMI between applications running
  69  * the same version of Swing.  As of 1.4, support for long term storage
  70  * of all JavaBeans&trade;
  71  * has been added to the {@code java.beans} package.
  72  * Please see {@link java.beans.XMLEncoder}.
  73  *
  74  * @see JScrollPane
  75  * @beaninfo
  76  *      attribute: isContainer false
  77  *    description: A component that helps determine the visible content range of an area.
  78  *
  79  * @author David Kloba
  80  * @since 1.2
  81  */
  82 @SuppressWarnings("serial") // Same-version serialization only
  83 public class JScrollBar extends JComponent implements Adjustable, Accessible
  84 {
  85     /**
  86      * @see #getUIClassID
  87      * @see #readObject
  88      */
  89     private static final String uiClassID = "ScrollBarUI";
  90 
  91     /**


 121     protected int blockIncrement;
 122 
 123 
 124     private void checkOrientation(int orientation) {
 125         switch (orientation) {
 126         case VERTICAL:
 127         case HORIZONTAL:
 128             break;
 129         default:
 130             throw new IllegalArgumentException("orientation must be one of: VERTICAL, HORIZONTAL");
 131         }
 132     }
 133 
 134 
 135     /**
 136      * Creates a scrollbar with the specified orientation,
 137      * value, extent, minimum, and maximum.
 138      * The "extent" is the size of the viewable area. It is also known
 139      * as the "visible amount".
 140      * <p>
 141      * Note: Use {@code setBlockIncrement} to set the block
 142      * increment to a size slightly smaller than the view's extent.
 143      * That way, when the user jumps the knob to an adjacent position,
 144      * one or two lines of the original contents remain in view.
 145      *
 146      * @exception IllegalArgumentException if orientation is not one of VERTICAL, HORIZONTAL
 147      *
 148      * @see #setOrientation
 149      * @see #setValue
 150      * @see #setVisibleAmount
 151      * @see #setMinimum
 152      * @see #setMaximum
 153      *
 154      * @param orientation an orientation of the {@code JScrollBar}
 155      * @param value an int giving the current value
 156      * @param extent an int giving the amount by which the value can "jump"
 157      * @param min an int giving the minimum value
 158      * @param max an int giving the maximum value
 159      */
 160     public JScrollBar(int orientation, int value, int extent, int min, int max)
 161     {


 187     }
 188 
 189 
 190     /**
 191      * Creates a vertical scrollbar with the following initial values:
 192      * <pre>
 193      * minimum = 0
 194      * maximum = 100
 195      * value = 0
 196      * extent = 10
 197      * </pre>
 198      */
 199     public JScrollBar() {
 200         this(VERTICAL);
 201     }
 202 
 203 
 204     /**
 205      * Sets the {@literal L&F} object that renders this component.
 206      *
 207      * @param ui  the {@code ScrollBarUI} {@literal L&F} object
 208      * @see UIDefaults#getUI
 209      * @since 1.4
 210      * @beaninfo
 211      *        bound: true
 212      *       hidden: true
 213      *    attribute: visualUpdate true
 214      *  description: The UI object that implements the Component's LookAndFeel
 215      */
 216     public void setUI(ScrollBarUI ui) {
 217         super.setUI(ui);
 218     }
 219 
 220 
 221     /**
 222      * Returns the delegate that implements the look and feel for
 223      * this component.
 224      *
 225      * @return the scroll bar's current UI.
 226      * @see JComponent#setUI
 227      */
 228     public ScrollBarUI getUI() {
 229         return (ScrollBarUI)ui;
 230     }
 231 
 232 
 233     /**
 234      * Overrides {@code JComponent.updateUI}.
 235      * @see JComponent#updateUI
 236      */
 237     public void updateUI() {
 238         setUI((ScrollBarUI)UIManager.getUI(this));
 239     }
 240 
 241 
 242     /**
 243      * Returns the name of the LookAndFeel class for this component.
 244      *
 245      * @return "ScrollBarUI"
 246      * @see JComponent#getUIClassID
 247      * @see UIDefaults#getUI
 248      */
 249     public String getUIClassID() {
 250         return uiClassID;
 251     }
 252 
 253 
 254 


 669      * @see #removeAdjustmentListener
 670      * @see BoundedRangeModel#addChangeListener
 671      */
 672     public void addAdjustmentListener(AdjustmentListener l)   {
 673         listenerList.add(AdjustmentListener.class, l);
 674     }
 675 
 676 
 677     /**
 678      * Removes an AdjustmentEvent listener.
 679      *
 680      * @param l the AdjustmentLister to remove
 681      * @see #addAdjustmentListener
 682      */
 683     public void removeAdjustmentListener(AdjustmentListener l)  {
 684         listenerList.remove(AdjustmentListener.class, l);
 685     }
 686 
 687 
 688     /**
 689      * Returns an array of all the {@code AdjustmentListener}s added
 690      * to this JScrollBar with addAdjustmentListener().
 691      *
 692      * @return all of the {@code AdjustmentListener}s added or an empty
 693      *         array if no listeners have been added
 694      * @since 1.4
 695      */
 696     public AdjustmentListener[] getAdjustmentListeners() {
 697         return listenerList.getListeners(AdjustmentListener.class);
 698     }
 699 
 700 
 701     /**
 702      * Notify listeners that the scrollbar's model has changed.
 703      *
 704      * @param id an integer indicating the type of event.
 705      * @param type an integer indicating the adjustment type.
 706      * @param value the current value of the adjustment
 707      *
 708      * @see #addAdjustmentListener
 709      * @see EventListenerList
 710      */
 711     protected void fireAdjustmentValueChanged(int id, int type, int value) {
 712         fireAdjustmentValueChanged(id, type, value, getValueIsAdjusting());


 801      * See readObject() and writeObject() in JComponent for more
 802      * information about serialization in Swing.
 803      */
 804     private void writeObject(ObjectOutputStream s) throws IOException {
 805         s.defaultWriteObject();
 806         if (getUIClassID().equals(uiClassID)) {
 807             byte count = JComponent.getWriteObjCounter(this);
 808             JComponent.setWriteObjCounter(this, --count);
 809             if (count == 0 && ui != null) {
 810                 ui.installUI(this);
 811             }
 812         }
 813     }
 814 
 815 
 816     /**
 817      * Returns a string representation of this JScrollBar. This method
 818      * is intended to be used only for debugging purposes, and the
 819      * content and format of the returned string may vary between
 820      * implementations. The returned string may be empty but may not
 821      * be {@code null}.
 822      *
 823      * @return  a string representation of this JScrollBar.
 824      */
 825     protected String paramString() {
 826         String orientationString = (orientation == HORIZONTAL ?
 827                                     "HORIZONTAL" : "VERTICAL");
 828 
 829         return super.paramString() +
 830         ",blockIncrement=" + blockIncrement +
 831         ",orientation=" + orientationString +
 832         ",unitIncrement=" + unitIncrement;
 833     }
 834 
 835 /////////////////
 836 // Accessibility support
 837 ////////////////
 838 
 839     /**
 840      * Gets the AccessibleContext associated with this JScrollBar.
 841      * For JScrollBar, the AccessibleContext takes the form of an
 842      * AccessibleJScrollBar.
 843      * A new AccessibleJScrollBar instance is created if necessary.
 844      *
 845      * @return an AccessibleJScrollBar that serves as the
 846      *         AccessibleContext of this JScrollBar
 847      */
 848     public AccessibleContext getAccessibleContext() {
 849         if (accessibleContext == null) {
 850             accessibleContext = new AccessibleJScrollBar();
 851         }
 852         return accessibleContext;
 853     }
 854 
 855     /**
 856      * This class implements accessibility support for the
 857      * {@code JScrollBar} class.  It provides an implementation of the
 858      * Java Accessibility API appropriate to scroll bar user-interface
 859      * elements.
 860      * <p>
 861      * <strong>Warning:</strong>
 862      * Serialized objects of this class will not be compatible with
 863      * future Swing releases. The current serialization support is
 864      * appropriate for short term storage or RMI between applications running
 865      * the same version of Swing.  As of 1.4, support for long term storage
 866      * of all JavaBeans&trade;
 867      * has been added to the {@code java.beans} package.
 868      * Please see {@link java.beans.XMLEncoder}.
 869      */
 870     @SuppressWarnings("serial") // Same-version serialization only
 871     protected class AccessibleJScrollBar extends AccessibleJComponent
 872         implements AccessibleValue {
 873 
 874         /**
 875          * Get the state set of this object.
 876          *
 877          * @return an instance of AccessibleState containing the current state
 878          * of the object
 879          * @see AccessibleState
 880          */
 881         public AccessibleStateSet getAccessibleStateSet() {
 882             AccessibleStateSet states = super.getAccessibleStateSet();
 883             if (getValueIsAdjusting()) {
 884                 states.add(AccessibleState.BUSY);
 885             }
 886             if (getOrientation() == VERTICAL) {
 887                 states.add(AccessibleState.VERTICAL);


< prev index next >