Module java.desktop
Package javax.swing

Class JSlider

  • All Implemented Interfaces:
    ImageObserver, MenuContainer, Serializable, Accessible, SwingConstants


    @JavaBean(defaultProperty="UI",
              description="A component that supports selecting a integer value from a range.")
    public class JSlider
    extends JComponent
    implements SwingConstants, Accessible
    A component that lets the user graphically select a value by sliding a knob within a bounded interval. The knob is always positioned at the points that match integer values within the specified interval.

    The slider can show both major tick marks, and minor tick marks between the major ones. The number of values between the tick marks is controlled with setMajorTickSpacing and setMinorTickSpacing. Painting of tick marks is controlled by setPaintTicks.

    Sliders can also print text labels at regular intervals (or at arbitrary locations) along the slider track. Painting of labels is controlled by setLabelTable and setPaintLabels.

    For further information and examples see How to Use Sliders, a section in The Java Tutorial.

    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 JavaBeans™ has been added to the java.beans package. Please see XMLEncoder.

    Since:
    1.2
    See Also:
    Serialized Form
    • Field Detail

      • sliderModel

        protected BoundedRangeModel sliderModel
        The data model that handles the numeric maximum value, minimum value, and current-position value for the slider.
      • majorTickSpacing

        protected int majorTickSpacing
        The number of values between the major tick marks -- the larger marks that break up the minor tick marks.
      • minorTickSpacing

        protected int minorTickSpacing
        The number of values between the minor tick marks -- the smaller marks that occur between the major tick marks.
        See Also:
        setMinorTickSpacing(int)
      • snapToTicks

        protected boolean snapToTicks
        If true, the knob (and the data value it represents) resolve to the closest tick mark next to where the user positioned the knob. The default is false.
        See Also:
        setSnapToTicks(boolean)
      • orientation

        protected int orientation
        Whether the slider is horizontal or vertical The default is horizontal.
        See Also:
        setOrientation(int)
      • changeEvent

        protected transient ChangeEvent changeEvent
        Only one ChangeEvent is needed per slider instance since the event's only (read-only) state is the source property. The source of events generated here is always "this". The event is lazily created the first time that an event notification is fired.
        See Also:
        fireStateChanged()
    • Constructor Detail

      • JSlider

        public JSlider​()
        Creates a horizontal slider with the range 0 to 100 and an initial value of 50.
      • JSlider

        public JSlider​(int orientation)
        Creates a slider using the specified orientation with the range 0 to 100 and an initial value of 50. The orientation can be either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL.
        Parameters:
        orientation - the orientation of the slider
        Throws:
        IllegalArgumentException - if orientation is not one of VERTICAL, HORIZONTAL
        See Also:
        setOrientation(int)
      • JSlider

        public JSlider​(int min,
                       int max)
        Creates a horizontal slider using the specified min and max with an initial value equal to the average of the min plus max.

        The BoundedRangeModel that holds the slider's data handles any issues that may arise from improperly setting the minimum and maximum values on the slider. See the BoundedRangeModel documentation for details.

        Parameters:
        min - the minimum value of the slider
        max - the maximum value of the slider
        See Also:
        BoundedRangeModel, setMinimum(int), setMaximum(int)
      • JSlider

        public JSlider​(int min,
                       int max,
                       int value)
        Creates a horizontal slider using the specified min, max and value.

        The BoundedRangeModel that holds the slider's data handles any issues that may arise from improperly setting the minimum, initial, and maximum values on the slider. See the BoundedRangeModel documentation for details.

        Parameters:
        min - the minimum value of the slider
        max - the maximum value of the slider
        value - the initial value of the slider
        See Also:
        BoundedRangeModel, setMinimum(int), setMaximum(int), setValue(int)
      • JSlider

        public JSlider​(int orientation,
                       int min,
                       int max,
                       int value)
        Creates a slider with the specified orientation and the specified minimum, maximum, and initial values. The orientation can be either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL.

        The BoundedRangeModel that holds the slider's data handles any issues that may arise from improperly setting the minimum, initial, and maximum values on the slider. See the BoundedRangeModel documentation for details.

        Parameters:
        orientation - the orientation of the slider
        min - the minimum value of the slider
        max - the maximum value of the slider
        value - the initial value of the slider
        Throws:
        IllegalArgumentException - if orientation is not one of VERTICAL, HORIZONTAL
        See Also:
        BoundedRangeModel, setOrientation(int), setMinimum(int), setMaximum(int), setValue(int)
      • JSlider

        public JSlider​(BoundedRangeModel brm)
        Creates a horizontal slider using the specified BoundedRangeModel.
        Parameters:
        brm - a BoundedRangeModel for the slider
    • Method Detail

      • getUI

        public SliderUI getUI​()
        Gets the UI object which implements the L&F for this component.
        Overrides:
        getUI in class JComponent
        Returns:
        the SliderUI object that implements the Slider L&F
      • createChangeListener

        protected ChangeListener createChangeListener​()
        Subclasses that want to handle ChangeEvents from the model differently can override this to return an instance of a custom ChangeListener implementation. The default ChangeListener simply calls the fireStateChanged method to forward ChangeEvents to the ChangeListeners that have been added directly to the slider.
        Returns:
        a instance of new ChangeListener
        See Also:
        changeListener, fireStateChanged(), ChangeListener, BoundedRangeModel
      • getChangeListeners

        @BeanProperty(bound=false)
        public ChangeListener[] getChangeListeners​()
        Returns an array of all the ChangeListeners added to this JSlider with addChangeListener().
        Returns:
        all of the ChangeListeners added or an empty array if no listeners have been added
        Since:
        1.4
      • fireStateChanged

        protected void fireStateChanged​()
        Send a ChangeEvent, whose source is this JSlider, to all ChangeListeners that have registered interest in ChangeEvents. This method is called each time a ChangeEvent is received from the model.

        The event instance is created if necessary, and stored in changeEvent.

        See Also:
        addChangeListener(javax.swing.event.ChangeListener), EventListenerList
      • setModel

        @BeanProperty(description="The sliders BoundedRangeModel.")
        public void setModel​(BoundedRangeModel newModel)
        Sets the BoundedRangeModel that handles the slider's three fundamental properties: minimum, maximum, value.

        Attempts to pass a null model to this method result in undefined behavior, and, most likely, exceptions.

        Parameters:
        newModel - the new, non-null BoundedRangeModel to use
        See Also:
        getModel(), BoundedRangeModel
      • getMinimum

        public int getMinimum​()
        Returns the minimum value supported by the slider from the BoundedRangeModel.
        Returns:
        the value of the model's minimum property
        See Also:
        setMinimum(int), BoundedRangeModel.getMinimum()
      • setMinimum

        @BeanProperty(preferred=true,
                      description="The sliders minimum value.")
        public void setMinimum​(int minimum)
        Sets the slider's minimum value to minimum. This method forwards the new minimum value to the model.

        The data model (an instance of BoundedRangeModel) handles any mathematical issues arising from assigning faulty values. See the BoundedRangeModel documentation for details.

        If the new minimum value is different from the previous minimum value, all change listeners are notified.

        Parameters:
        minimum - the new minimum
        See Also:
        getMinimum(), addChangeListener(javax.swing.event.ChangeListener), BoundedRangeModel.setMinimum(int)
      • getMaximum

        public int getMaximum​()
        Returns the maximum value supported by the slider from the BoundedRangeModel.
        Returns:
        the value of the model's maximum property
        See Also:
        setMaximum(int), BoundedRangeModel.getMaximum()
      • setMaximum

        @BeanProperty(preferred=true,
                      description="The sliders maximum value.")
        public void setMaximum​(int maximum)
        Sets the slider's maximum value to maximum. This method forwards the new maximum value to the model.

        The data model (an instance of BoundedRangeModel) handles any mathematical issues arising from assigning faulty values. See the BoundedRangeModel documentation for details.

        If the new maximum value is different from the previous maximum value, all change listeners are notified.

        Parameters:
        maximum - the new maximum
        See Also:
        getMaximum(), addChangeListener(javax.swing.event.ChangeListener), BoundedRangeModel.setMaximum(int)
      • getValueIsAdjusting

        public boolean getValueIsAdjusting​()
        Returns the valueIsAdjusting property from the model. For details on how this is used, see the setValueIsAdjusting documentation.
        Returns:
        the value of the model's valueIsAdjusting property
        See Also:
        setValueIsAdjusting(boolean)
      • getExtent

        public int getExtent​()
        Returns the "extent" from the BoundedRangeModel. This represents the range of values "covered" by the knob.
        Returns:
        an int representing the extent
        See Also:
        setExtent(int), BoundedRangeModel.getExtent()
      • setExtent

        @BeanProperty(bound=false,
                      expert=true,
                      description="Size of the range covered by the knob.")
        public void setExtent​(int extent)
        Sets the size of the range "covered" by the knob. Most look and feel implementations will change the value by this amount if the user clicks on either side of the knob. This method just forwards the new extent value to the model.

        The data model (an instance of BoundedRangeModel) handles any mathematical issues arising from assigning faulty values. See the BoundedRangeModel documentation for details.

        If the new extent value is different from the previous extent value, all change listeners are notified.

        Parameters:
        extent - the new extent
        See Also:
        getExtent(), BoundedRangeModel.setExtent(int)
      • getOrientation

        public int getOrientation​()
        Return this slider's vertical or horizontal orientation.
        Returns:
        SwingConstants.VERTICAL or SwingConstants.HORIZONTAL
        See Also:
        setOrientation(int)
      • setOrientation

        @BeanProperty(preferred=true,
                      visualUpdate=true,
                      enumerationValues={"JSlider.VERTICAL","JSlider.HORIZONTAL"},
                      description="Set the scrollbars orientation to either VERTICAL or HORIZONTAL.")
        public void setOrientation​(int orientation)
        Set the slider's orientation to either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL.
        Parameters:
        orientation - HORIZONTAL or VERTICAL
        Throws:
        IllegalArgumentException - if orientation is not one of VERTICAL, HORIZONTAL
        See Also:
        getOrientation()
      • setFont

        public void setFont​(Font font)
        Sets the font for this component.
        Overrides:
        setFont in class JComponent
        Parameters:
        font - the desired Font for this component
        Since:
        1.6
        See Also:
        Component.getFont()
      • getLabelTable

        public Dictionary getLabelTable​()
        Returns the dictionary of what labels to draw at which values.
        Returns:
        the Dictionary containing labels and where to draw them
      • setLabelTable

        @BeanProperty(hidden=true,
                      visualUpdate=true,
                      description="Specifies what labels will be drawn for any given value.")
        public void setLabelTable​(Dictionary labels)
        Used to specify what label will be drawn at any given value. The key-value pairs are of this format: { Integer value, java.swing.JComponent label }.

        An easy way to generate a standard table of value labels is by using the createStandardLabels method.

        Once the labels have been set, this method calls updateLabelUIs(). Note that the labels are only painted if the paintLabels property is true.

        Parameters:
        labels - new Dictionary of labels, or null to remove all labels
        See Also:
        createStandardLabels(int), getLabelTable(), setPaintLabels(boolean)
      • updateLabelUIs

        protected void updateLabelUIs​()
        Updates the UIs for the labels in the label table by calling updateUI on each label. The UIs are updated from the current look and feel. The labels are also set to their preferred size.
        See Also:
        setLabelTable(java.util.Dictionary), JComponent.updateUI()
      • createStandardLabels

        public Hashtable<Integer,JComponent> createStandardLabels​(int increment)
        Creates a Hashtable of numerical text labels, starting at the slider minimum, and using the increment specified. For example, if you call createStandardLabels( 10 ) and the slider minimum is zero, then labels will be created for the values 0, 10, 20, 30, and so on.

        For the labels to be drawn on the slider, the returned Hashtable must be passed into setLabelTable, and setPaintLabels must be set to true.

        For further details on the makeup of the returned Hashtable, see the setLabelTable documentation.

        Parameters:
        increment - distance between labels in the generated hashtable
        Returns:
        a new Hashtable of labels
        Throws:
        IllegalArgumentException - if increment is less than or equal to zero
        See Also:
        setLabelTable(java.util.Dictionary), setPaintLabels(boolean)
      • createStandardLabels

        public Hashtable<Integer,JComponent> createStandardLabels​(int increment,
                                                                  int start)
        Creates a Hashtable of numerical text labels, starting at the starting point specified, and using the increment specified. For example, if you call createStandardLabels( 10, 2 ), then labels will be created for the values 2, 12, 22, 32, and so on.

        For the labels to be drawn on the slider, the returned Hashtable must be passed into setLabelTable, and setPaintLabels must be set to true.

        For further details on the makeup of the returned Hashtable, see the setLabelTable documentation.

        Parameters:
        increment - distance between labels in the generated hashtable
        start - value at which the labels will begin
        Returns:
        a new Hashtable of labels
        Throws:
        IllegalArgumentException - if start is out of range, or if increment is less than or equal to zero
        See Also:
        setLabelTable(java.util.Dictionary), setPaintLabels(boolean)
      • getInverted

        public boolean getInverted​()
        Returns true if the value-range shown for the slider is reversed,
        Returns:
        true if the slider values are reversed from their normal order
        See Also:
        setInverted(boolean)
      • setInverted

        @BeanProperty(visualUpdate=true,
                      description="If true reverses the slider values from their normal order")
        public void setInverted​(boolean b)
        Specify true to reverse the value-range shown for the slider and false to put the value range in the normal order. The order depends on the slider's ComponentOrientation property. Normal (non-inverted) horizontal sliders with a ComponentOrientation value of LEFT_TO_RIGHT have their maximum on the right. Normal horizontal sliders with a ComponentOrientation value of RIGHT_TO_LEFT have their maximum on the left. Normal vertical sliders have their maximum on the top. These labels are reversed when the slider is inverted.

        By default, the value of this property is false.

        Parameters:
        b - true to reverse the slider values from their normal order
      • getMajorTickSpacing

        public int getMajorTickSpacing​()
        This method returns the major tick spacing. The number that is returned represents the distance, measured in values, between each major tick mark. If you have a slider with a range from 0 to 50 and the major tick spacing is set to 10, you will get major ticks next to the following values: 0, 10, 20, 30, 40, 50.
        Returns:
        the number of values between major ticks
        See Also:
        setMajorTickSpacing(int)
      • setMajorTickSpacing

        @BeanProperty(visualUpdate=true,
                      description="Sets the number of values between major tick marks.")
        public void setMajorTickSpacing​(int n)
        This method sets the major tick spacing. The number that is passed in represents the distance, measured in values, between each major tick mark. If you have a slider with a range from 0 to 50 and the major tick spacing is set to 10, you will get major ticks next to the following values: 0, 10, 20, 30, 40, 50.

        In order for major ticks to be painted, setPaintTicks must be set to true.

        This method will also set up a label table for you. If there is not already a label table, and the major tick spacing is > 0, and getPaintLabels returns true, a standard label table will be generated (by calling createStandardLabels) with labels at the major tick marks. For the example above, you would get text labels: "0", "10", "20", "30", "40", "50". The label table is then set on the slider by calling setLabelTable.

        Parameters:
        n - new value for the majorTickSpacing property
        See Also:
        getMajorTickSpacing(), setPaintTicks(boolean), setLabelTable(java.util.Dictionary), createStandardLabels(int)
      • getMinorTickSpacing

        public int getMinorTickSpacing​()
        This method returns the minor tick spacing. The number that is returned represents the distance, measured in values, between each minor tick mark. If you have a slider with a range from 0 to 50 and the minor tick spacing is set to 10, you will get minor ticks next to the following values: 0, 10, 20, 30, 40, 50.
        Returns:
        the number of values between minor ticks
        See Also:
        getMinorTickSpacing()
      • setMinorTickSpacing

        @BeanProperty(visualUpdate=true,
                      description="Sets the number of values between minor tick marks.")
        public void setMinorTickSpacing​(int n)
        This method sets the minor tick spacing. The number that is passed in represents the distance, measured in values, between each minor tick mark. If you have a slider with a range from 0 to 50 and the minor tick spacing is set to 10, you will get minor ticks next to the following values: 0, 10, 20, 30, 40, 50.

        In order for minor ticks to be painted, setPaintTicks must be set to true.

        Parameters:
        n - new value for the minorTickSpacing property
        See Also:
        getMinorTickSpacing(), setPaintTicks(boolean)
      • getSnapToTicks

        public boolean getSnapToTicks​()
        Returns true if the knob (and the data value it represents) resolve to the closest tick mark next to where the user positioned the knob.
        Returns:
        true if the value snaps to the nearest tick mark, else false
        See Also:
        setSnapToTicks(boolean)
      • setSnapToTicks

        @BeanProperty(description="If true snap the knob to the nearest tick mark.")
        public void setSnapToTicks​(boolean b)
        Specifying true makes the knob (and the data value it represents) resolve to the closest tick mark next to where the user positioned the knob. By default, this property is false.
        Parameters:
        b - true to snap the knob to the nearest tick mark
        See Also:
        getSnapToTicks()
      • getPaintTicks

        public boolean getPaintTicks​()
        Tells if tick marks are to be painted.
        Returns:
        true if tick marks are painted, else false
        See Also:
        setPaintTicks(boolean)
      • setPaintTicks

        @BeanProperty(visualUpdate=true,
                      description="If true tick marks are painted on the slider.")
        public void setPaintTicks​(boolean b)
        Determines whether tick marks are painted on the slider. By default, this property is false.
        Parameters:
        b - whether or not tick marks should be painted
        See Also:
        getPaintTicks()
      • getPaintTrack

        public boolean getPaintTrack​()
        Tells if the track (area the slider slides in) is to be painted.
        Returns:
        true if track is painted, else false
        See Also:
        setPaintTrack(boolean)
      • setPaintTrack

        @BeanProperty(visualUpdate=true,
                      description="If true, the track is painted on the slider.")
        public void setPaintTrack​(boolean b)
        Determines whether the track is painted on the slider. By default, this property is true. It is up to the look and feel to honor this property, some may choose to ignore it.
        Parameters:
        b - whether or not to paint the slider track
        See Also:
        getPaintTrack()
      • getPaintLabels

        public boolean getPaintLabels​()
        Tells if labels are to be painted.
        Returns:
        true if labels are painted, else false
        See Also:
        setPaintLabels(boolean)
      • setPaintLabels

        @BeanProperty(visualUpdate=true,
                      description="If true labels are painted on the slider.")
        public void setPaintLabels​(boolean b)
        Determines whether labels are painted on the slider.

        This method will also set up a label table for you. If there is not already a label table, and the major tick spacing is > 0, a standard label table will be generated (by calling createStandardLabels) with labels at the major tick marks. The label table is then set on the slider by calling setLabelTable.

        By default, this property is false.

        Parameters:
        b - whether or not to paint labels
        See Also:
        getPaintLabels(), getLabelTable(), createStandardLabels(int)
      • paramString

        protected String paramString​()
        Returns a string representation of this JSlider. 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.
        Overrides:
        paramString in class JComponent
        Returns:
        a string representation of this JSlider.
      • getAccessibleContext

        @BeanProperty(bound=false)
        public AccessibleContext getAccessibleContext​()
        Gets the AccessibleContext associated with this JSlider. For sliders, the AccessibleContext takes the form of an AccessibleJSlider. A new AccessibleJSlider instance is created if necessary.
        Specified by:
        getAccessibleContext in interface Accessible
        Overrides:
        getAccessibleContext in class Component
        Returns:
        an AccessibleJSlider that serves as the AccessibleContext of this JSlider