2 Copyright (C) 2002, 2004, 2005, 2006, Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import gnu
.java
.lang
.CPStringBuilder
;
43 import java
.awt
.MenuContainer
;
44 import java
.awt
.image
.ImageObserver
;
45 import java
.beans
.PropertyChangeEvent
;
46 import java
.io
.Serializable
;
47 import java
.util
.Dictionary
;
48 import java
.util
.Enumeration
;
49 import java
.util
.Hashtable
;
51 import javax
.accessibility
.Accessible
;
52 import javax
.accessibility
.AccessibleContext
;
53 import javax
.accessibility
.AccessibleRole
;
54 import javax
.accessibility
.AccessibleState
;
55 import javax
.accessibility
.AccessibleStateSet
;
56 import javax
.accessibility
.AccessibleValue
;
57 import javax
.swing
.event
.ChangeEvent
;
58 import javax
.swing
.event
.ChangeListener
;
59 import javax
.swing
.plaf
.SliderUI
;
60 import javax
.swing
.plaf
.UIResource
;
63 * A visual component that allows selection of a value within a
64 * range by adjusting a thumb in a track. The values for the minimum,
65 * maximum, extent and value are stored in a {@link
66 * DefaultBoundedRangeModel}.
68 * A <code>JSlider</code> component has the following properties:
72 * <tr><th> Property </th><th> Stored in </th><th> Bound? </th></tr>
73 * <tr><td> extent </td><td> model </td><td> no </td></tr>
74 * <tr><td> inverted </td><td> slider </td><td> yes </td></tr>
75 * <tr><td> labelTable </td><td> slider </td><td> yes </td></tr>
76 * <tr><td> majorTickSpacing </td><td> slider </td><td> yes </td></tr>
77 * <tr><td> maximum </td><td> model </td><td> yes </td></tr>
78 * <tr><td> minimum </td><td> model </td><td> yes </td></tr>
79 * <tr><td> minorTickSpacing </td><td> slider </td><td> yes </td></tr>
80 * <tr><td> model </td><td> slider </td><td> yes </td></tr>
81 * <tr><td> orientation </td><td> slider </td><td> yes </td></tr>
82 * <tr><td> paintLabels </td><td> slider </td><td> yes </td></tr>
83 * <tr><td> paintTicks </td><td> slider </td><td> yes </td></tr>
84 * <tr><td> snapToTicks </td><td> slider </td><td> yes </td></tr>
85 * <tr><td> value </td><td> model </td><td> no </td></tr>
86 * <tr><td> valueIsAdjusting </td><td> model </td><td> no </td></tr>
90 * The various behavioural aspects of these properties follows:
95 * When a non-bound property stored in the slider changes, the slider fires
96 * a {@link ChangeEvent} to its change listeners.
99 * When a bound property stored in the slider changes, the slider fires a
100 * {@link PropertyChangeEvent} to its property change listeners.
103 * If any of the model's properties change, it fires a {@link ChangeEvent} to
104 * its listeners, which include the slider.
107 * If the slider receives a {@link ChangeEvent} from its model, it will
108 * propagate the event to its own change listeners, with the event's "source"
109 * property set to refer to the slider, rather than the model.
113 public class JSlider
extends JComponent
implements SwingConstants
, Accessible
,
115 MenuContainer
, Serializable
119 * A little testing shows that the reference implementation creates
120 * labels from a class named LabelUIResource.
122 private class LabelUIResource
124 implements UIResource
126 LabelUIResource(String text
, int align
)
129 setName("Slider.label");
133 private static final long serialVersionUID
= -1441275936141218479L;
136 * Provides the accessibility features for the <code>JSlider</code>
139 protected class AccessibleJSlider
extends JComponent
.AccessibleJComponent
140 implements AccessibleValue
142 private static final long serialVersionUID
= -6301740148041106789L;
145 * Creates a new <code>AccessibleJSlider</code> instance.
147 protected AccessibleJSlider()
149 // Nothing to do here.
153 * Returns a set containing the current state of the {@link JSlider}
156 * @return The accessible state set.
158 public AccessibleStateSet
getAccessibleStateSet()
160 AccessibleStateSet result
= super.getAccessibleStateSet();
161 if (orientation
== JSlider
.HORIZONTAL
)
162 result
.add(AccessibleState
.HORIZONTAL
);
163 else if (orientation
== JSlider
.VERTICAL
)
164 result
.add(AccessibleState
.VERTICAL
);
169 * Returns the accessible role for the <code>JSlider</code> component.
171 * @return {@link AccessibleRole#SLIDER}.
173 public AccessibleRole
getAccessibleRole()
175 return AccessibleRole
.SLIDER
;
179 * Returns an object that provides access to the current, minimum and
180 * maximum values for the {@link JSlider}. Since this class implements
181 * {@link AccessibleValue}, it returns itself.
183 * @return The accessible value.
185 public AccessibleValue
getAccessibleValue()
191 * Returns the current value of the {@link JSlider} component, as an
194 * @return The current value of the {@link JSlider} component.
196 public Number
getCurrentAccessibleValue()
198 return new Integer(getValue());
202 * Sets the current value of the {@link JSlider} component and sends a
203 * {@link PropertyChangeEvent} (with the property name
204 * {@link AccessibleContext#ACCESSIBLE_VALUE_PROPERTY}) to all registered
205 * listeners. If the supplied value is <code>null</code>, this method
206 * does nothing and returns <code>false</code>.
208 * @param value the new slider value (<code>null</code> permitted).
210 * @return <code>true</code> if the slider value is updated, and
211 * <code>false</code> otherwise.
213 public boolean setCurrentAccessibleValue(Number value
)
217 Number oldValue
= getCurrentAccessibleValue();
218 setValue(value
.intValue());
219 firePropertyChange(AccessibleContext
.ACCESSIBLE_VALUE_PROPERTY
, oldValue
,
220 new Integer(getValue()));
225 * Returns the minimum value of the {@link JSlider} component, as an
228 * @return The minimum value of the {@link JSlider} component.
230 public Number
getMinimumAccessibleValue()
232 return new Integer(getMinimum());
236 * Returns the maximum value of the {@link JSlider} component, as an
239 * @return The maximum value of the {@link JSlider} component.
241 public Number
getMaximumAccessibleValue()
243 return new Integer(getMaximum());
247 /** Whether or not this slider paints its ticks. */
248 private transient boolean paintTicks
;
250 /** Whether or not this slider paints its track. */
251 private transient boolean paintTrack
= true;
253 /** Whether or not this slider paints its labels. */
254 private transient boolean paintLabels
;
257 * A dictionary of (Integer, Component) pairs where each Component is a
258 * JLabel and the Integer determines where the label will be painted.
260 private transient Dictionary labelTable
;
262 /** The model used to store the slider's range and current value. */
263 protected BoundedRangeModel sliderModel
;
265 /** The space/distance between major ticks. */
266 protected int majorTickSpacing
;
268 /** The space/distance between minor ticks. */
269 protected int minorTickSpacing
;
271 /** Whether the slider snaps its values to ticks. */
272 protected boolean snapToTicks
;
274 /** The orientation (horizontal or vertical) of the slider. */
275 protected int orientation
= HORIZONTAL
;
277 /** Whether the slider is inverted. */
278 private transient boolean isInverted
;
281 * The listener that monitors the slider's model and forwards events to the
282 * slider's listeners (see <code>createChangeListener()</code>).
284 protected ChangeListener changeListener
;
286 /** The change event that is passed to all listeners of this slider. */
287 protected transient ChangeEvent changeEvent
;
290 * Creates a new horizontal <code>JSlider</code> instance with a minimum of
291 * 0, a maximum of 100, and a value of 50.
295 this(HORIZONTAL
, 0, 100, 50);
299 * Creates a new <code>JSlider</code> instance with the given orientation
300 * and a minimum of 0, a maximum of 100, and a value of 50.
302 * @param orientation The orientation of the slider ({@link #HORIZONTAL} or
303 * {@link #VERTICAL}).
305 * @throws IllegalArgumentException if <code>orientation</code> is not one of
306 * the specified values.
308 public JSlider(int orientation
)
310 this(orientation
, 0, 100, 50);
314 * Creates a new horizontal <code>JSlider</code> instance with the given
315 * maximum and minimum and a value that is halfway between the minimum and the
318 * @param minimum The minimum value.
319 * @param maximum The maximum value.
321 * @throws IllegalArgumentException if <code>minimum</code> is greater than
322 * <code>maximum</code>.
324 public JSlider(int minimum
, int maximum
)
326 this(HORIZONTAL
, minimum
, maximum
, (maximum
+ minimum
) / 2);
330 * Creates a new horizontal <code>JSlider</code> instance with the given
331 * minimum, maximum, and value.
333 * @param minimum The minimum value.
334 * @param maximum The maximum value.
335 * @param value The initial value.
337 * @throws IllegalArgumentException if <code>value</code> is not in the
339 * @throws IllegalArgumentException if <code>minimum</code> is greater than
340 * <code>maximum</code>.
342 public JSlider(int minimum
, int maximum
, int value
)
344 this(HORIZONTAL
, minimum
, maximum
, value
);
348 * Creates a new <code>JSlider</code> instance with the given orientation,
349 * minimum, maximum, and value.
351 * @param orientation The orientation of the slider ({@link #HORIZONTAL} or
352 * {@link #VERTICAL}).
353 * @param minimum The minimum value of the JSlider.
354 * @param maximum The maximum value of the JSlider.
355 * @param value The initial value of the JSlider.
357 * @throws IllegalArgumentException if <code>orientation</code> is not one of
358 * the specified values.
359 * @throws IllegalArgumentException if <code>value</code> is not in the
361 * @throws IllegalArgumentException if <code>minimum</code> is greater than
362 * <code>maximum</code>.
364 public JSlider(int orientation
, int minimum
, int maximum
, int value
)
366 sliderModel
= new DefaultBoundedRangeModel(value
, 0, minimum
, maximum
);
367 if (orientation
!= HORIZONTAL
&& orientation
!= VERTICAL
)
368 throw new IllegalArgumentException(orientation
369 + " is not a legal orientation");
370 this.orientation
= orientation
;
371 changeListener
= createChangeListener();
372 sliderModel
.addChangeListener(changeListener
);
377 * Creates a new horizontal <code>JSlider</code> instance with the given
380 * @param model The model (<code>null</code> not permitted).
382 * @throws NullPointerException if <code>model</code> is <code>null</code>.
384 public JSlider(BoundedRangeModel model
)
387 changeListener
= createChangeListener();
388 sliderModel
.addChangeListener(changeListener
);
393 * Returns the slider's value (from the slider's model).
395 * @return The value of the slider.
397 * @see #setValue(int)
399 public int getValue()
401 return sliderModel
.getValue();
405 * Sets the slider's value and sends a {@link ChangeEvent} to all
406 * registered listeners. Note that the model will fire a change event to all
407 * of its registered listeners first (with the model as the event source) and
408 * then the slider will fire another change event to all of its registered
409 * listeners (this time with the slider as the event source).
411 * @param value the new value.
415 public void setValue(int value
)
417 sliderModel
.setValue(value
);
421 * Returns the slider's UI delegate.
423 * @return The slider's UI delegate.
425 public SliderUI
getUI()
427 return (SliderUI
) ui
;
431 * Sets the slider's UI delegate.
433 * @param ui the UI delegate.
435 public void setUI(SliderUI ui
)
441 * Sets this slider's UI delegate to the default (obtained from the
442 * {@link UIManager}) for the current look and feel.
444 public void updateUI()
447 setUI((SliderUI
) UIManager
.getUI(this));
451 * Returns the suffix (<code>"SliderUI"</code> in this case) used to
452 * determine the class name for a UI delegate that can provide the look and
453 * feel for a <code>JSlider</code>.
455 * @return <code>"SliderUI"</code>.
457 public String
getUIClassID()
463 * Creates a {@link ChangeListener} that is added to the slider's model and
464 * forwards change events generated by the model to the listeners that are
465 * registered with the <code>JSlider</code> (by calling the
466 * {@link #fireStateChanged} method).
468 * @return A new listener.
470 protected ChangeListener
createChangeListener()
472 return new ChangeListener()
474 public void stateChanged(ChangeEvent ce
)
476 // No need to trigger a repaint since the UI listens to the model
477 // as well. All we need to do is pass on the stateChanged event
485 * Registers a listener with the slider so that it will receive
486 * {@link ChangeEvent} notifications. Note that change events generated
487 * by the slider's model will be forwarded automatically to the slider's
490 * @param listener the listener to register.
492 * @see #removeChangeListener(ChangeListener)
494 public void addChangeListener(ChangeListener listener
)
496 listenerList
.add(ChangeListener
.class, listener
);
500 * Removes a listener from this slider so that it will no longer receive
501 * {@link ChangeEvent} notifications from the slider.
503 * @param listener The listener to remove.
505 * @see #addChangeListener(ChangeListener)
507 public void removeChangeListener(ChangeListener listener
)
509 listenerList
.remove(ChangeListener
.class, listener
);
513 * Sends a {@link ChangeEvent} to all registered listeners, with this slider
516 protected void fireStateChanged()
518 Object
[] changeListeners
= listenerList
.getListenerList();
519 if (changeEvent
== null)
520 changeEvent
= new ChangeEvent(this);
521 for (int i
= changeListeners
.length
- 2; i
>= 0; i
-= 2)
523 if (changeListeners
[i
] == ChangeListener
.class)
524 ((ChangeListener
) changeListeners
[i
+ 1]).stateChanged(changeEvent
);
529 * Returns an array containing all the {@link ChangeListener} instances
530 * registered with this slider. If no listeners are registered, this method
531 * returns an empty array.
533 * @return An array array containing all the {@link ChangeListener} instances
534 * registered with this slider (possibly empty, but never
535 * <code>null</code>).
537 public ChangeListener
[] getChangeListeners()
539 return (ChangeListener
[]) listenerList
.getListeners(ChangeListener
.class);
543 * Returns the slider's model, which stores the minimum, maximum and current
546 * @return The slider's model.
548 * @see #setModel(BoundedRangeModel)
550 public BoundedRangeModel
getModel()
556 * Sets the slider's model and sends a {@link PropertyChangeEvent} (with the
557 * property name "model") to all registered listeners. The change listener
558 * that the slider registered with the original model is removed and added
559 * to the new model (this ensures that {@link ChangeEvent} notifications
560 * generated by the model are automatically forwarded to listeners that are
561 * registered with the slider).
563 * @param model The model to use with the slider.
567 public void setModel(BoundedRangeModel model
)
569 // I didn't do the null pointer check on purpose.
570 // If you try it with Sun's, it'll go ahead and set it to null
571 // and bork the next time it tries to access the model.
572 if (model
!= sliderModel
)
574 BoundedRangeModel oldModel
= sliderModel
;
576 oldModel
.removeChangeListener(changeListener
);
577 sliderModel
.addChangeListener(changeListener
);
578 firePropertyChange("model", oldModel
, sliderModel
);
583 * Returns the minimum value of the slider (from the slider's model).
585 * @return The minimum value of the slider.
587 * @see #setMinimum(int)
589 public int getMinimum()
591 return sliderModel
.getMinimum();
595 * Sets the minimum value of the slider and fires a
596 * {@link PropertyChangeEvent} (with the property name "minimum") to all
597 * registered listeners. Note that:
600 * <li>the minimum value is stored in the slider's model (see
601 * {@link #getModel()});</li>
602 * <li>in addition to the property change event, the slider also fires a
603 * {@link ChangeEvent}.</li>
606 * @param minimum The minimum value of the slider.
610 public void setMinimum(int minimum
)
612 int old
= sliderModel
.getMinimum();
613 sliderModel
.setMinimum(minimum
);
615 firePropertyChange("minimum", old
, minimum
);
619 * Returns the slider's maximum value (obtained from the slider's model).
621 * @return The maximum value of the slider.
623 * @see #setMaximum(int)
625 public int getMaximum()
627 return sliderModel
.getMaximum();
631 * Sets the maximum value of the slider and fires a
632 * {@link PropertyChangeEvent} (with the property name "maximum") to all
633 * registered listeners. Note that:
636 * <li>the maximum value is stored in the slider's model (see
637 * {@link #getModel()});</li>
638 * <li>in addition to the property change event, the slider also fires a
639 * {@link ChangeEvent}.</li>
642 * @param maximum The maximum value of the slider.
646 public void setMaximum(int maximum
)
648 int old
= sliderModel
.getMaximum();
649 sliderModel
.setMaximum(maximum
);
651 firePropertyChange("maximum", old
, maximum
);
655 * Returns the <code>valueIsAdjusting</code> flag from the slider's model.
657 * @return The <code>valueIsAdjusting</code> flag from the slider's model.
659 * @see #setValueIsAdjusting(boolean)
661 public boolean getValueIsAdjusting()
663 return sliderModel
.getValueIsAdjusting();
667 * Sets the <code>valueIsAdjusting</code> flag in the slider's model, and
668 * sends a {@link ChangeEvent} to all registered listeners.
670 * @param adjusting the new flag value.
672 * @see #getValueIsAdjusting()
674 public void setValueIsAdjusting(boolean adjusting
)
676 sliderModel
.setValueIsAdjusting(adjusting
);
680 * Returns the slider's extent value, obtained from the slider's model.
682 * @return The extent value.
684 * @see #setExtent(int)
686 public int getExtent()
688 return sliderModel
.getExtent();
692 * Sets the slider's extent value and sends a {@link ChangeEvent} to all
693 * registered listeners. Note that the model will fire a change event to all
694 * of its registered listeners first (with the model as the event source) and
695 * then the slider will fire another change event to all of its registered
696 * listeners (this time with the slider as the event source).
698 * @param extent The extent value for this slider.
702 public void setExtent(int extent
)
704 sliderModel
.setExtent(extent
);
708 * Returns the orientation of the slider, either {@link JSlider#HORIZONTAL}
709 * or {@link JSlider#VERTICAL}.
711 * @return The orientation of the slider.
713 * @see #setOrientation(int)
715 public int getOrientation()
721 * Sets the orientation for the slider and sends a
722 * {@link PropertyChangeEvent} (with the property name "orientation") to all
723 * registered listeners.
725 * @param orientation the orientation (one of {@link JSlider#HORIZONTAL} or
726 * {@link JSlider#VERTICAL}).
728 * @throws IllegalArgumentException if <code>orientation</code> is not one of
729 * the permitted values.
731 * @see #getOrientation()
733 public void setOrientation(int orientation
)
735 if (orientation
!= VERTICAL
&& orientation
!= HORIZONTAL
)
736 throw new IllegalArgumentException(
737 "orientation must be one of: VERTICAL, HORIZONTAL");
738 if (orientation
!= this.orientation
)
740 int oldOrientation
= this.orientation
;
741 this.orientation
= orientation
;
742 firePropertyChange("orientation", oldOrientation
, this.orientation
);
748 * Returns the label table for the slider.
750 * @return The label table for the slider (possibly <code>null</code>).
752 * @see #setLabelTable(Dictionary)
754 public Dictionary
getLabelTable()
760 * Sets the table of labels for the slider and sends a
761 * {@link PropertyChangeEvent} (with the property name "labelTable") to all
762 * registered listeners.
764 * @param table the table of labels (<code>null</code> permitted).
766 * @see #getLabelTable()
768 public void setLabelTable(Dictionary table
)
770 if (table
!= labelTable
)
772 Dictionary oldTable
= labelTable
;
775 firePropertyChange("labelTable", oldTable
, labelTable
);
782 * Resets the UI delegates for the labels in the <code>labelTable</code> to
783 * the default for the current look and feel.
785 protected void updateLabelUIs()
787 if (labelTable
!= null)
789 for (Enumeration list
= labelTable
.elements(); list
.hasMoreElements();)
791 Object o
= list
.nextElement();
792 if (o
instanceof JComponent
)
794 JComponent jc
= (JComponent
) o
;
796 jc
.setSize(jc
.getPreferredSize());
803 * Creates a hashtable of <code>(Integer, JLabel)</code> pairs that can be
804 * used as a label table for this slider. The labels will start from the
805 * slider's minimum and increase by the increment. Each label will have a text
806 * string indicating its integer value.
808 * @param increment The increment between labels (must be > 0).
810 * @return A hashtable containing the labels.
812 * @throws IllegalArgumentException if <code>increment</code> is not greater
815 public Hashtable
createStandardLabels(int increment
)
817 return createStandardLabels(increment
, sliderModel
.getMinimum());
821 * Creates a hashtable of <code>(Integer, JLabel)</code> pairs that can be
822 * used as a label table for this slider. The labels will start from the
823 * given start value and increase by the increment. Each label will have a
824 * text string indicating its integer value.
826 * @param increment The increment between labels (must be > 0).
827 * @param start The value to start from.
829 * @return A hashtable with the labels and their keys.
831 * @throws IllegalArgumentException if <code>increment</code> is not greater
832 * than zero, or <code>start</code> is not within the range of the
835 public Hashtable
createStandardLabels(int increment
, int start
)
838 throw new IllegalArgumentException("Requires 'increment' > 0.");
839 if (start
< getMinimum() || start
> getMaximum())
840 throw new IllegalArgumentException("The 'start' value is out of range.");
841 Hashtable table
= new Hashtable();
842 int max
= getMaximum();
843 for (int i
= start
; i
<= max
; i
+= increment
)
845 LabelUIResource label
= new LabelUIResource(String
.valueOf(i
),
847 table
.put(new Integer(i
), label
);
853 * Returns the flag that controls whether or not the value scale for the
854 * slider is inverted (the default value is <code>false</code>).
856 * @return The flag that controls whether or not the value scale for the
857 * slider is inverted.
859 * @see #setInverted(boolean)
861 public boolean getInverted()
867 * Sets the flag that controls whether or not the value scale for the
868 * slider is inverted and, if the new flag value is different to the old flag
869 * value, sends a {@link PropertyChangeEvent} to all registered listeners.
870 * Typically, a horizontal slider will display a scale that increases from
871 * left to right, but this is reversed if the 'inverted' flag is set to
872 * <code>true</code>. Similarly, a vertical slider will display a scale that
873 * increases from bottom to top, and this is reversed if the 'inverted' flag
874 * is set to <code>true</code>.
876 * @param inverted the new flag value.
878 * @see #getInverted()
880 public void setInverted(boolean inverted
)
882 if (isInverted
!= inverted
)
884 boolean oldInverted
= isInverted
;
885 isInverted
= inverted
;
886 firePropertyChange("inverted", oldInverted
, isInverted
);
892 * Returns the distance between major tick marks along the slider's value
895 * @return The amount of units between each major tick mark.
897 * @see #setMajorTickSpacing(int)
899 public int getMajorTickSpacing()
901 return majorTickSpacing
;
905 * Sets the distance between major tick marks along the slider's value scale,
906 * and sends a {@link PropertyChangeEvent} (with the property name
907 * "majorTickSpacing") to all registered listeners.
909 * @param spacing the distance between major tick marks.
911 * @see #getMajorTickSpacing()
913 public void setMajorTickSpacing(int spacing
)
915 if (majorTickSpacing
!= spacing
)
917 int oldSpacing
= majorTickSpacing
;
918 majorTickSpacing
= spacing
;
919 if (labelTable
== null && majorTickSpacing
> 0 && getPaintLabels())
920 setLabelTable(createStandardLabels(majorTickSpacing
));
921 firePropertyChange("majorTickSpacing", oldSpacing
, majorTickSpacing
);
928 * Returns the distance between minor tick marks along the slider's value
931 * @return The distance between minor tick marks along the slider's value
934 * @see #setMinorTickSpacing(int)
936 public int getMinorTickSpacing()
938 return minorTickSpacing
;
942 * Sets the distance between minor tick marks along the slider's value scale,
943 * and sends a {@link PropertyChangeEvent} (with the property name
944 * "minorTickSpacing") to all registered listeners.
946 * @param spacing the distance between minor tick marks.
948 * @see #getMinorTickSpacing()
950 public void setMinorTickSpacing(int spacing
)
952 if (minorTickSpacing
!= spacing
)
954 int oldSpacing
= minorTickSpacing
;
955 minorTickSpacing
= spacing
;
956 firePropertyChange("minorTickSpacing", oldSpacing
, minorTickSpacing
);
963 * Returns the flag that controls whether the slider thumb will snap to ticks.
964 * Sliders that snap to ticks will automatically move the thumb to the
967 * @return <code>true</code> if the slider thumb automatically.
969 * @see #setSnapToTicks(boolean)
971 public boolean getSnapToTicks()
977 * Sets the flag that controls whether the slider thumb will snap to ticks
978 * and sends a {@link PropertyChangeEvent} (with the property name
979 * 'snapToTicks') to all registered listeners. Sliders that snap to ticks
980 * will automatically move the thumb to the nearest tick mark.
982 * @param snap the new flag value.
984 * @see #getSnapToTicks()
986 public void setSnapToTicks(boolean snap
)
988 if (snap
!= snapToTicks
)
991 firePropertyChange("snapToTicks", !snap
, snap
);
996 * Returns the flag that controls whether or not tick marks are painted along
997 * the slider's value scale.
999 * @return <code>true</code> if tick marks should be painted, and
1000 * <code>false</code> if tick marks should not be painted.
1002 * @see #setPaintTicks(boolean)
1004 public boolean getPaintTicks()
1010 * Sets the flag that controls whether or not tick marks are painted along
1011 * the slider's value scale, and sends a {@link PropertyChangeEvent} (with
1012 * the property name "paintTicks") to all registered listeners. In
1013 * addition to setting this property to <code>true</code>, one or both of the
1014 * minor tick spacing and major tick spacing attributes must be set to a
1015 * value greater than 0 in order for ticks to be painted.
1017 * @param paint Whether ticks will be painted.
1019 * @see #getPaintTicks()
1021 public void setPaintTicks(boolean paint
)
1023 if (paint
!= paintTicks
)
1025 boolean oldPaintTicks
= paintTicks
;
1027 firePropertyChange("paintTicks", oldPaintTicks
, paintTicks
);
1034 * Returns the flag that controls whether or not the track is painted.
1036 * @return Whether the track will be painted.
1038 * @see #setPaintTrack(boolean)
1040 public boolean getPaintTrack()
1046 * Sets the flag that controls whether or not the track is painted, and
1047 * sends a {@link PropertyChangeEvent} (for the "paintTrack" property) to all
1048 * registered listeners.
1050 * @param paint Whether the track will be painted.
1052 * @see #getPaintTrack()
1054 public void setPaintTrack(boolean paint
)
1056 if (paintTrack
!= paint
)
1059 firePropertyChange("paintTrack", !paint
, paint
);
1065 * Returns the flag that controls whether or not labels are painted for the
1066 * tick marks along the slider.
1068 * @return Whether labels will be painted.
1070 * @see #setPaintLabels(boolean)
1072 public boolean getPaintLabels()
1078 * Sets the flag that controls whether or not labels are painted for the
1079 * tick marks along the slider and sends a {@link PropertyChangeEvent} (with
1080 * the property name "paintLabels") to all registered listeners.
1082 * @param paint Whether labels will be painted.
1084 * @see #getPaintLabels()
1086 public void setPaintLabels(boolean paint
)
1088 if (paint
!= paintLabels
)
1090 paintLabels
= paint
;
1091 if (paint
&& majorTickSpacing
> 0 && labelTable
== null)
1092 setLabelTable(createStandardLabels(majorTickSpacing
));
1093 firePropertyChange("paintLabels", !paint
, paint
);
1100 * Returns an implementation-dependent string describing the attributes of
1101 * this <code>JSlider</code>.
1103 * @return A string describing the attributes of this <code>JSlider</code>
1104 * (never <code>null</code>).
1106 protected String
paramString()
1108 String superParamStr
= super.paramString();
1109 CPStringBuilder sb
= new CPStringBuilder();
1110 sb
.append(",isInverted=").append(getInverted());
1111 sb
.append(",majorTickSpacing=").append(getMajorTickSpacing());
1112 sb
.append(",minorTickSpacing=").append(getMinorTickSpacing());
1113 sb
.append(",orientation=");
1114 if (orientation
== HORIZONTAL
)
1115 sb
.append("HORIZONTAL");
1117 sb
.append("VERTICAL");
1118 sb
.append(",paintLabels=").append(getPaintLabels());
1119 sb
.append(",paintTicks=").append(getPaintTicks());
1120 sb
.append(",paintTrack=").append(getPaintTrack());
1121 sb
.append(",snapToTicks=").append(getSnapToTicks());
1123 // the following is output by the reference implementation. We don't
1124 // strictly need to replicate this. Perhaps it has some meaning, but
1125 // I couldn't determine it yet...
1126 sb
.append(",snapToValue=true");
1128 return superParamStr
+ sb
.toString();
1132 * Returns the object that provides accessibility features for this
1133 * <code>JSlider</code> component.
1135 * @return The accessible context (an instance of {@link AccessibleJSlider}).
1137 public AccessibleContext
getAccessibleContext()
1139 if (accessibleContext
== null)
1140 accessibleContext
= new AccessibleJSlider();
1142 return accessibleContext
;