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 java.awt.MenuContainer;
42 import java.awt.image.ImageObserver;
43 import java.beans.PropertyChangeEvent;
44 import java.io.Serializable;
45 import java.util.Dictionary;
46 import java.util.Enumeration;
47 import java.util.Hashtable;
49 import javax.accessibility.Accessible;
50 import javax.accessibility.AccessibleContext;
51 import javax.accessibility.AccessibleRole;
52 import javax.accessibility.AccessibleState;
53 import javax.accessibility.AccessibleStateSet;
54 import javax.accessibility.AccessibleValue;
55 import javax.swing.event.ChangeEvent;
56 import javax.swing.event.ChangeListener;
57 import javax.swing.plaf.SliderUI;
58 import javax.swing.plaf.UIResource;
61 * A visual component that allows selection of a value within a
62 * range by adjusting a thumb in a track. The values for the minimum,
63 * maximum, extent and value are stored in a {@link
64 * DefaultBoundedRangeModel}.
66 * A <code>JSlider</code> component has the following properties:
70 * <tr><th> Property </th><th> Stored in </th><th> Bound? </th></tr>
71 * <tr><td> extent </td><td> model </td><td> no </td></tr>
72 * <tr><td> inverted </td><td> slider </td><td> yes </td></tr>
73 * <tr><td> labelTable </td><td> slider </td><td> yes </td></tr>
74 * <tr><td> majorTickSpacing </td><td> slider </td><td> yes </td></tr>
75 * <tr><td> maximum </td><td> model </td><td> yes </td></tr>
76 * <tr><td> minimum </td><td> model </td><td> yes </td></tr>
77 * <tr><td> minorTickSpacing </td><td> slider </td><td> yes </td></tr>
78 * <tr><td> model </td><td> slider </td><td> yes </td></tr>
79 * <tr><td> orientation </td><td> slider </td><td> yes </td></tr>
80 * <tr><td> paintLabels </td><td> slider </td><td> yes </td></tr>
81 * <tr><td> paintTicks </td><td> slider </td><td> yes </td></tr>
82 * <tr><td> snapToTicks </td><td> slider </td><td> yes </td></tr>
83 * <tr><td> value </td><td> model </td><td> no </td></tr>
84 * <tr><td> valueIsAdjusting </td><td> model </td><td> no </td></tr>
88 * The various behavioural aspects of these properties follows:
93 * When a non-bound property stored in the slider changes, the slider fires
94 * a {@link ChangeEvent} to its change listeners.
97 * When a bound property stored in the slider changes, the slider fires a
98 * {@link PropertyChangeEvent} to its property change listeners.
101 * If any of the model's properties change, it fires a {@link ChangeEvent} to
102 * its listeners, which include the slider.
105 * If the slider receives a {@link ChangeEvent} from its model, it will
106 * propagate the event to its own change listeners, with the event's "source"
107 * property set to refer to the slider, rather than the model.
111 public class JSlider extends JComponent implements SwingConstants, Accessible,
113 MenuContainer, Serializable
117 * A little testing shows that the reference implementation creates
118 * labels from a class named LabelUIResource.
120 private class LabelUIResource
122 implements UIResource
124 LabelUIResource(String text, int align)
127 setName("Slider.label");
131 private static final long serialVersionUID = -1441275936141218479L;
134 * Provides the accessibility features for the <code>JSlider</code>
137 protected class AccessibleJSlider extends JComponent.AccessibleJComponent
138 implements AccessibleValue
140 private static final long serialVersionUID = -6301740148041106789L;
143 * Creates a new <code>AccessibleJSlider</code> instance.
145 protected AccessibleJSlider()
147 // Nothing to do here.
151 * Returns a set containing the current state of the {@link JSlider}
154 * @return The accessible state set.
156 public AccessibleStateSet getAccessibleStateSet()
158 AccessibleStateSet result = super.getAccessibleStateSet();
159 if (orientation == JSlider.HORIZONTAL)
160 result.add(AccessibleState.HORIZONTAL);
161 else if (orientation == JSlider.VERTICAL)
162 result.add(AccessibleState.VERTICAL);
167 * Returns the accessible role for the <code>JSlider</code> component.
169 * @return {@link AccessibleRole#SLIDER}.
171 public AccessibleRole getAccessibleRole()
173 return AccessibleRole.SLIDER;
177 * Returns an object that provides access to the current, minimum and
178 * maximum values for the {@link JSlider}. Since this class implements
179 * {@link AccessibleValue}, it returns itself.
181 * @return The accessible value.
183 public AccessibleValue getAccessibleValue()
189 * Returns the current value of the {@link JSlider} component, as an
192 * @return The current value of the {@link JSlider} component.
194 public Number getCurrentAccessibleValue()
196 return new Integer(getValue());
200 * Sets the current value of the {@link JSlider} component and sends a
201 * {@link PropertyChangeEvent} (with the property name
202 * {@link AccessibleContext#ACCESSIBLE_VALUE_PROPERTY}) to all registered
203 * listeners. If the supplied value is <code>null</code>, this method
204 * does nothing and returns <code>false</code>.
206 * @param value the new slider value (<code>null</code> permitted).
208 * @return <code>true</code> if the slider value is updated, and
209 * <code>false</code> otherwise.
211 public boolean setCurrentAccessibleValue(Number value)
215 Number oldValue = getCurrentAccessibleValue();
216 setValue(value.intValue());
217 firePropertyChange(AccessibleContext.ACCESSIBLE_VALUE_PROPERTY, oldValue,
218 new Integer(getValue()));
223 * Returns the minimum value of the {@link JSlider} component, as an
226 * @return The minimum value of the {@link JSlider} component.
228 public Number getMinimumAccessibleValue()
230 return new Integer(getMinimum());
234 * Returns the maximum value of the {@link JSlider} component, as an
237 * @return The maximum value of the {@link JSlider} component.
239 public Number getMaximumAccessibleValue()
241 return new Integer(getMaximum());
245 /** Whether or not this slider paints its ticks. */
246 private transient boolean paintTicks;
248 /** Whether or not this slider paints its track. */
249 private transient boolean paintTrack = true;
251 /** Whether or not this slider paints its labels. */
252 private transient boolean paintLabels;
255 * A dictionary of (Integer, Component) pairs where each Component is a
256 * JLabel and the Integer determines where the label will be painted.
258 private transient Dictionary labelTable;
260 /** The model used to store the slider's range and current value. */
261 protected BoundedRangeModel sliderModel;
263 /** The space/distance between major ticks. */
264 protected int majorTickSpacing;
266 /** The space/distance between minor ticks. */
267 protected int minorTickSpacing;
269 /** Whether the slider snaps its values to ticks. */
270 protected boolean snapToTicks;
272 /** The orientation (horizontal or vertical) of the slider. */
273 protected int orientation = HORIZONTAL;
275 /** Whether the slider is inverted. */
276 private transient boolean isInverted;
279 * The listener that monitors the slider's model and forwards events to the
280 * slider's listeners (see <code>createChangeListener()</code>).
282 protected ChangeListener changeListener;
284 /** The change event that is passed to all listeners of this slider. */
285 protected transient ChangeEvent changeEvent;
288 * Creates a new horizontal <code>JSlider</code> instance with a minimum of
289 * 0, a maximum of 100, and a value of 50.
293 this(HORIZONTAL, 0, 100, 50);
297 * Creates a new <code>JSlider</code> instance with the given orientation
298 * and a minimum of 0, a maximum of 100, and a value of 50.
300 * @param orientation The orientation of the slider ({@link #HORIZONTAL} or
301 * {@link #VERTICAL}).
303 * @throws IllegalArgumentException if <code>orientation</code> is not one of
304 * the specified values.
306 public JSlider(int orientation)
308 this(orientation, 0, 100, 50);
312 * Creates a new horizontal <code>JSlider</code> instance with the given
313 * maximum and minimum and a value that is halfway between the minimum and the
316 * @param minimum The minimum value.
317 * @param maximum The maximum value.
319 * @throws IllegalArgumentException if <code>minimum</code> is greater than
320 * <code>maximum</code>.
322 public JSlider(int minimum, int maximum)
324 this(HORIZONTAL, minimum, maximum, (maximum + minimum) / 2);
328 * Creates a new horizontal <code>JSlider</code> instance with the given
329 * minimum, maximum, and value.
331 * @param minimum The minimum value.
332 * @param maximum The maximum value.
333 * @param value The initial value.
335 * @throws IllegalArgumentException if <code>value</code> is not in the
337 * @throws IllegalArgumentException if <code>minimum</code> is greater than
338 * <code>maximum</code>.
340 public JSlider(int minimum, int maximum, int value)
342 this(HORIZONTAL, minimum, maximum, value);
346 * Creates a new <code>JSlider</code> instance with the given orientation,
347 * minimum, maximum, and value.
349 * @param orientation The orientation of the slider ({@link #HORIZONTAL} or
350 * {@link #VERTICAL}).
351 * @param minimum The minimum value of the JSlider.
352 * @param maximum The maximum value of the JSlider.
353 * @param value The initial value of the JSlider.
355 * @throws IllegalArgumentException if <code>orientation</code> is not one of
356 * the specified values.
357 * @throws IllegalArgumentException if <code>value</code> is not in the
359 * @throws IllegalArgumentException if <code>minimum</code> is greater than
360 * <code>maximum</code>.
362 public JSlider(int orientation, int minimum, int maximum, int value)
364 sliderModel = new DefaultBoundedRangeModel(value, 0, minimum, maximum);
365 if (orientation != HORIZONTAL && orientation != VERTICAL)
366 throw new IllegalArgumentException(orientation
367 + " is not a legal orientation");
368 this.orientation = orientation;
369 changeListener = createChangeListener();
370 sliderModel.addChangeListener(changeListener);
375 * Creates a new horizontal <code>JSlider</code> instance with the given
378 * @param model The model (<code>null</code> not permitted).
380 * @throws NullPointerException if <code>model</code> is <code>null</code>.
382 public JSlider(BoundedRangeModel model)
385 changeListener = createChangeListener();
386 sliderModel.addChangeListener(changeListener);
391 * Returns the slider's value (from the slider's model).
393 * @return The value of the slider.
395 * @see #setValue(int)
397 public int getValue()
399 return sliderModel.getValue();
403 * Sets the slider's value and sends a {@link ChangeEvent} to all
404 * registered listeners. Note that the model will fire a change event to all
405 * of its registered listeners first (with the model as the event source) and
406 * then the slider will fire another change event to all of its registered
407 * listeners (this time with the slider as the event source).
409 * @param value the new value.
413 public void setValue(int value)
415 sliderModel.setValue(value);
419 * Returns the slider's UI delegate.
421 * @return The slider's UI delegate.
423 public SliderUI getUI()
425 return (SliderUI) ui;
429 * Sets the slider's UI delegate.
431 * @param ui the UI delegate.
433 public void setUI(SliderUI ui)
439 * Sets this slider's UI delegate to the default (obtained from the
440 * {@link UIManager}) for the current look and feel.
442 public void updateUI()
445 setUI((SliderUI) UIManager.getUI(this));
449 * Returns the suffix (<code>"SliderUI"</code> in this case) used to
450 * determine the class name for a UI delegate that can provide the look and
451 * feel for a <code>JSlider</code>.
453 * @return <code>"SliderUI"</code>.
455 public String getUIClassID()
461 * Creates a {@link ChangeListener} that is added to the slider's model and
462 * forwards change events generated by the model to the listeners that are
463 * registered with the <code>JSlider</code> (by calling the
464 * {@link #fireStateChanged} method).
466 * @return A new listener.
468 protected ChangeListener createChangeListener()
470 return new ChangeListener()
472 public void stateChanged(ChangeEvent ce)
474 // No need to trigger a repaint since the UI listens to the model
475 // as well. All we need to do is pass on the stateChanged event
483 * Registers a listener with the slider so that it will receive
484 * {@link ChangeEvent} notifications. Note that change events generated
485 * by the slider's model will be forwarded automatically to the slider's
488 * @param listener the listener to register.
490 * @see #removeChangeListener(ChangeListener)
492 public void addChangeListener(ChangeListener listener)
494 listenerList.add(ChangeListener.class, listener);
498 * Removes a listener from this slider so that it will no longer receive
499 * {@link ChangeEvent} notifications from the slider.
501 * @param listener The listener to remove.
503 * @see #addChangeListener(ChangeListener)
505 public void removeChangeListener(ChangeListener listener)
507 listenerList.remove(ChangeListener.class, listener);
511 * Sends a {@link ChangeEvent} to all registered listeners, with this slider
514 protected void fireStateChanged()
516 Object[] changeListeners = listenerList.getListenerList();
517 if (changeEvent == null)
518 changeEvent = new ChangeEvent(this);
519 for (int i = changeListeners.length - 2; i >= 0; i -= 2)
521 if (changeListeners[i] == ChangeListener.class)
522 ((ChangeListener) changeListeners[i + 1]).stateChanged(changeEvent);
527 * Returns an array containing all the {@link ChangeListener} instances
528 * registered with this slider. If no listeners are registered, this method
529 * returns an empty array.
531 * @return An array array containing all the {@link ChangeListener} instances
532 * registered with this slider (possibly empty, but never
533 * <code>null</code>).
535 public ChangeListener[] getChangeListeners()
537 return (ChangeListener[]) listenerList.getListeners(ChangeListener.class);
541 * Returns the slider's model, which stores the minimum, maximum and current
544 * @return The slider's model.
546 * @see #setModel(BoundedRangeModel)
548 public BoundedRangeModel getModel()
554 * Sets the slider's model and sends a {@link PropertyChangeEvent} (with the
555 * property name "model") to all registered listeners. The change listener
556 * that the slider registered with the original model is removed and added
557 * to the new model (this ensures that {@link ChangeEvent} notifications
558 * generated by the model are automatically forwarded to listeners that are
559 * registered with the slider).
561 * @param model The model to use with the slider.
565 public void setModel(BoundedRangeModel model)
567 // I didn't do the null pointer check on purpose.
568 // If you try it with Sun's, it'll go ahead and set it to null
569 // and bork the next time it tries to access the model.
570 if (model != sliderModel)
572 BoundedRangeModel oldModel = sliderModel;
574 oldModel.removeChangeListener(changeListener);
575 sliderModel.addChangeListener(changeListener);
576 firePropertyChange("model", oldModel, sliderModel);
581 * Returns the minimum value of the slider (from the slider's model).
583 * @return The minimum value of the slider.
585 * @see #setMinimum(int)
587 public int getMinimum()
589 return sliderModel.getMinimum();
593 * Sets the minimum value of the slider and fires a
594 * {@link PropertyChangeEvent} (with the property name "minimum") to all
595 * registered listeners. Note that:
598 * <li>the minimum value is stored in the slider's model (see
599 * {@link #getModel()});</li>
600 * <li>in addition to the property change event, the slider also fires a
601 * {@link ChangeEvent}.</li>
604 * @param minimum The minimum value of the slider.
608 public void setMinimum(int minimum)
610 int old = sliderModel.getMinimum();
611 sliderModel.setMinimum(minimum);
613 firePropertyChange("minimum", old, minimum);
617 * Returns the slider's maximum value (obtained from the slider's model).
619 * @return The maximum value of the slider.
621 * @see #setMaximum(int)
623 public int getMaximum()
625 return sliderModel.getMaximum();
629 * Sets the maximum value of the slider and fires a
630 * {@link PropertyChangeEvent} (with the property name "maximum") to all
631 * registered listeners. Note that:
634 * <li>the maximum value is stored in the slider's model (see
635 * {@link #getModel()});</li>
636 * <li>in addition to the property change event, the slider also fires a
637 * {@link ChangeEvent}.</li>
640 * @param maximum The maximum value of the slider.
644 public void setMaximum(int maximum)
646 int old = sliderModel.getMaximum();
647 sliderModel.setMaximum(maximum);
649 firePropertyChange("maximum", old, maximum);
653 * Returns the <code>valueIsAdjusting</code> flag from the slider's model.
655 * @return The <code>valueIsAdjusting</code> flag from the slider's model.
657 * @see #setValueIsAdjusting(boolean)
659 public boolean getValueIsAdjusting()
661 return sliderModel.getValueIsAdjusting();
665 * Sets the <code>valueIsAdjusting</code> flag in the slider's model, and
666 * sends a {@link ChangeEvent} to all registered listeners.
668 * @param adjusting the new flag value.
670 * @see #getValueIsAdjusting()
672 public void setValueIsAdjusting(boolean adjusting)
674 sliderModel.setValueIsAdjusting(adjusting);
678 * Returns the slider's extent value, obtained from the slider's model.
680 * @return The extent value.
682 * @see #setExtent(int)
684 public int getExtent()
686 return sliderModel.getExtent();
690 * Sets the slider's extent value and sends a {@link ChangeEvent} to all
691 * registered listeners. Note that the model will fire a change event to all
692 * of its registered listeners first (with the model as the event source) and
693 * then the slider will fire another change event to all of its registered
694 * listeners (this time with the slider as the event source).
696 * @param extent The extent value for this slider.
700 public void setExtent(int extent)
702 sliderModel.setExtent(extent);
706 * Returns the orientation of the slider, either {@link JSlider#HORIZONTAL}
707 * or {@link JSlider#VERTICAL}.
709 * @return The orientation of the slider.
711 * @see #setOrientation(int)
713 public int getOrientation()
719 * Sets the orientation for the slider and sends a
720 * {@link PropertyChangeEvent} (with the property name "orientation") to all
721 * registered listeners.
723 * @param orientation the orientation (one of {@link JSlider#HORIZONTAL} or
724 * {@link JSlider#VERTICAL}).
726 * @throws IllegalArgumentException if <code>orientation</code> is not one of
727 * the permitted values.
729 * @see #getOrientation()
731 public void setOrientation(int orientation)
733 if (orientation != VERTICAL && orientation != HORIZONTAL)
734 throw new IllegalArgumentException(
735 "orientation must be one of: VERTICAL, HORIZONTAL");
736 if (orientation != this.orientation)
738 int oldOrientation = this.orientation;
739 this.orientation = orientation;
740 firePropertyChange("orientation", oldOrientation, this.orientation);
746 * Returns the label table for the slider.
748 * @return The label table for the slider (possibly <code>null</code>).
750 * @see #setLabelTable(Dictionary)
752 public Dictionary getLabelTable()
758 * Sets the table of labels for the slider and sends a
759 * {@link PropertyChangeEvent} (with the property name "labelTable") to all
760 * registered listeners.
762 * @param table the table of labels (<code>null</code> permitted).
764 * @see #getLabelTable()
766 public void setLabelTable(Dictionary table)
768 if (table != labelTable)
770 Dictionary oldTable = labelTable;
773 firePropertyChange("labelTable", oldTable, labelTable);
780 * Resets the UI delegates for the labels in the <code>labelTable</code> to
781 * the default for the current look and feel.
783 protected void updateLabelUIs()
785 if (labelTable != null)
787 for (Enumeration list = labelTable.elements(); list.hasMoreElements();)
789 Object o = list.nextElement();
790 if (o instanceof JComponent)
792 JComponent jc = (JComponent) o;
794 jc.setSize(jc.getPreferredSize());
801 * Creates a hashtable of <code>(Integer, JLabel)</code> pairs that can be
802 * used as a label table for this slider. The labels will start from the
803 * slider's minimum and increase by the increment. Each label will have a text
804 * string indicating its integer value.
806 * @param increment The increment between labels (must be > 0).
808 * @return A hashtable containing the labels.
810 * @throws IllegalArgumentException if <code>increment</code> is not greater
813 public Hashtable createStandardLabels(int increment)
815 return createStandardLabels(increment, sliderModel.getMinimum());
819 * Creates a hashtable of <code>(Integer, JLabel)</code> pairs that can be
820 * used as a label table for this slider. The labels will start from the
821 * given start value and increase by the increment. Each label will have a
822 * text string indicating its integer value.
824 * @param increment The increment between labels (must be > 0).
825 * @param start The value to start from.
827 * @return A hashtable with the labels and their keys.
829 * @throws IllegalArgumentException if <code>increment</code> is not greater
830 * than zero, or <code>start</code> is not within the range of the
833 public Hashtable createStandardLabels(int increment, int start)
836 throw new IllegalArgumentException("Requires 'increment' > 0.");
837 if (start < getMinimum() || start > getMaximum())
838 throw new IllegalArgumentException("The 'start' value is out of range.");
839 Hashtable table = new Hashtable();
840 int max = getMaximum();
841 for (int i = start; i <= max; i += increment)
843 LabelUIResource label = new LabelUIResource(String.valueOf(i),
845 table.put(new Integer(i), label);
851 * Returns the flag that controls whether or not the value scale for the
852 * slider is inverted (the default value is <code>false</code>).
854 * @return The flag that controls whether or not the value scale for the
855 * slider is inverted.
857 * @see #setInverted(boolean)
859 public boolean getInverted()
865 * Sets the flag that controls whether or not the value scale for the
866 * slider is inverted and, if the new flag value is different to the old flag
867 * value, sends a {@link PropertyChangeEvent} to all registered listeners.
868 * Typically, a horizontal slider will display a scale that increases from
869 * left to right, but this is reversed if the 'inverted' flag is set to
870 * <code>true</code>. Similarly, a vertical slider will display a scale that
871 * increases from bottom to top, and this is reversed if the 'inverted' flag
872 * is set to <code>true</code>.
874 * @param inverted the new flag value.
876 * @see #getInverted()
878 public void setInverted(boolean inverted)
880 if (isInverted != inverted)
882 boolean oldInverted = isInverted;
883 isInverted = inverted;
884 firePropertyChange("inverted", oldInverted, isInverted);
890 * Returns the distance between major tick marks along the slider's value
893 * @return The amount of units between each major tick mark.
895 * @see #setMajorTickSpacing(int)
897 public int getMajorTickSpacing()
899 return majorTickSpacing;
903 * Sets the distance between major tick marks along the slider's value scale,
904 * and sends a {@link PropertyChangeEvent} (with the property name
905 * "majorTickSpacing") to all registered listeners.
907 * @param spacing the distance between major tick marks.
909 * @see #getMajorTickSpacing()
911 public void setMajorTickSpacing(int spacing)
913 if (majorTickSpacing != spacing)
915 int oldSpacing = majorTickSpacing;
916 majorTickSpacing = spacing;
917 if (labelTable == null && majorTickSpacing > 0 && getPaintLabels())
918 setLabelTable(createStandardLabels(majorTickSpacing));
919 firePropertyChange("majorTickSpacing", oldSpacing, majorTickSpacing);
926 * Returns the distance between minor tick marks along the slider's value
929 * @return The distance between minor tick marks along the slider's value
932 * @see #setMinorTickSpacing(int)
934 public int getMinorTickSpacing()
936 return minorTickSpacing;
940 * Sets the distance between minor tick marks along the slider's value scale,
941 * and sends a {@link PropertyChangeEvent} (with the property name
942 * "minorTickSpacing") to all registered listeners.
944 * @param spacing the distance between minor tick marks.
946 * @see #getMinorTickSpacing()
948 public void setMinorTickSpacing(int spacing)
950 if (minorTickSpacing != spacing)
952 int oldSpacing = minorTickSpacing;
953 minorTickSpacing = spacing;
954 firePropertyChange("minorTickSpacing", oldSpacing, minorTickSpacing);
961 * Returns the flag that controls whether the slider thumb will snap to ticks.
962 * Sliders that snap to ticks will automatically move the thumb to the
965 * @return <code>true</code> if the slider thumb automatically.
967 * @see #setSnapToTicks(boolean)
969 public boolean getSnapToTicks()
975 * Sets the flag that controls whether the slider thumb will snap to ticks
976 * and sends a {@link PropertyChangeEvent} (with the property name
977 * 'snapToTicks') to all registered listeners. Sliders that snap to ticks
978 * will automatically move the thumb to the nearest tick mark.
980 * @param snap the new flag value.
982 * @see #getSnapToTicks()
984 public void setSnapToTicks(boolean snap)
986 if (snap != snapToTicks)
989 firePropertyChange("snapToTicks", !snap, snap);
994 * Returns the flag that controls whether or not tick marks are painted along
995 * the slider's value scale.
997 * @return <code>true</code> if tick marks should be painted, and
998 * <code>false</code> if tick marks should not be painted.
1000 * @see #setPaintTicks(boolean)
1002 public boolean getPaintTicks()
1008 * Sets the flag that controls whether or not tick marks are painted along
1009 * the slider's value scale, and sends a {@link PropertyChangeEvent} (with
1010 * the property name "paintTicks") to all registered listeners. In
1011 * addition to setting this property to <code>true</code>, one or both of the
1012 * minor tick spacing and major tick spacing attributes must be set to a
1013 * value greater than 0 in order for ticks to be painted.
1015 * @param paint Whether ticks will be painted.
1017 * @see #getPaintTicks()
1019 public void setPaintTicks(boolean paint)
1021 if (paint != paintTicks)
1023 boolean oldPaintTicks = paintTicks;
1025 firePropertyChange("paintTicks", oldPaintTicks, paintTicks);
1032 * Returns the flag that controls whether or not the track is painted.
1034 * @return Whether the track will be painted.
1036 * @see #setPaintTrack(boolean)
1038 public boolean getPaintTrack()
1044 * Sets the flag that controls whether or not the track is painted, and
1045 * sends a {@link PropertyChangeEvent} (for the "paintTrack" property) to all
1046 * registered listeners.
1048 * @param paint Whether the track will be painted.
1050 * @see #getPaintTrack()
1052 public void setPaintTrack(boolean paint)
1054 if (paintTrack != paint)
1057 firePropertyChange("paintTrack", !paint, paint);
1063 * Returns the flag that controls whether or not labels are painted for the
1064 * tick marks along the slider.
1066 * @return Whether labels will be painted.
1068 * @see #setPaintLabels(boolean)
1070 public boolean getPaintLabels()
1076 * Sets the flag that controls whether or not labels are painted for the
1077 * tick marks along the slider and sends a {@link PropertyChangeEvent} (with
1078 * the property name "paintLabels") to all registered listeners.
1080 * @param paint Whether labels will be painted.
1082 * @see #getPaintLabels()
1084 public void setPaintLabels(boolean paint)
1086 if (paint != paintLabels)
1088 paintLabels = paint;
1089 if (paint && majorTickSpacing > 0 && labelTable == null)
1090 setLabelTable(createStandardLabels(majorTickSpacing));
1091 firePropertyChange("paintLabels", !paint, paint);
1098 * Returns an implementation-dependent string describing the attributes of
1099 * this <code>JSlider</code>.
1101 * @return A string describing the attributes of this <code>JSlider</code>
1102 * (never <code>null</code>).
1104 protected String paramString()
1106 String superParamStr = super.paramString();
1107 StringBuffer sb = new StringBuffer();
1108 sb.append(",isInverted=").append(getInverted());
1109 sb.append(",majorTickSpacing=").append(getMajorTickSpacing());
1110 sb.append(",minorTickSpacing=").append(getMinorTickSpacing());
1111 sb.append(",orientation=");
1112 if (orientation == HORIZONTAL)
1113 sb.append("HORIZONTAL");
1115 sb.append("VERTICAL");
1116 sb.append(",paintLabels=").append(getPaintLabels());
1117 sb.append(",paintTicks=").append(getPaintTicks());
1118 sb.append(",paintTrack=").append(getPaintTrack());
1119 sb.append(",snapToTicks=").append(getSnapToTicks());
1121 // the following is output by the reference implementation. We don't
1122 // strictly need to replicate this. Perhaps it has some meaning, but
1123 // I couldn't determine it yet...
1124 sb.append(",snapToValue=true");
1126 return superParamStr + sb.toString();
1130 * Returns the object that provides accessibility features for this
1131 * <code>JSlider</code> component.
1133 * @return The accessible context (an instance of {@link AccessibleJSlider}).
1135 public AccessibleContext getAccessibleContext()
1137 if (accessibleContext == null)
1138 accessibleContext = new AccessibleJSlider();
1140 return accessibleContext;