Java Swing | JSlider

15 Apr, 2021
JSlider is a part of Java Swing package . JSlider is an implementation of slider. The Component allows the user to select a value by sliding the knob within the bounded value . The slider can show Major Tick marks and also the minor tick marks between two major tick marks, The knob can be positioned at only those points. Constructor of the class are :  
JSlider() : create a new slider with horizontal orientation and max and min value 100 and 0 respectively and the slider value is set to 50.JSlider(BoundedRangeModel b) : creates a new Slider with horizontal orientation and a specified boundary range model.JSlider(int o) : create a new slider with a specified orientation and max and min value 100 and 0 respectively and the slider value is set to 50.JSlider(int min, int max) :create a new slider with horizontal orientation and max and min value specified and the slider value is set to the average of max and min value.JSlider(int min, int max, int value) :create a new slider with horizontal orientation and max, min value and the slider Value specified .JSlider(int o, int min, int max, int value) : create a new slider with orientation and max, min value and the slider Value specified .
JSlider() : create a new slider with horizontal orientation and max and min value 100 and 0 respectively and the slider value is set to 50.
JSlider(BoundedRangeModel b) : creates a new Slider with horizontal orientation and a specified boundary range model.
JSlider(int o) : create a new slider with a specified orientation and max and min value 100 and 0 respectively and the slider value is set to 50.
JSlider(int min, int max) :create a new slider with horizontal orientation and max and min value specified and the slider value is set to the average of max and min value.
JSlider(int min, int max, int value) :create a new slider with horizontal orientation and max, min value and the slider Value specified .
JSlider(int o, int min, int max, int value) : create a new slider with orientation and max, min value and the slider Value specified .
Commonly used functions  
setOrientation(int n) : sets the orientation of the slider to the specified value  setValue( int v) : sets the value of the slider to the specified value  setPaintTicks(boolean b) : the boolean value determines whether the ticks are painted on the slider or not  setPaintTrack(boolean b) : the boolean value determines whether the track are painted on the slider or not  setMajorTickSpacing(int n) : sets the spacing for major ticks .  setMinorTickSpacing(int n) : sets the spacing for minor ticks .  setFont(Font f) : set the font of the text for the slider  setMaximum(int m) : set the maximum value for the slider  setMinimum(int m) : sets the minimum value for the slider  updateUI() : Resets the UI property to a value from the current look and feel.  setValueIsAdjusting(boolean b) : Sets the model’s valueIsAdjusting property to boolean b.  setUI(SliderUI ui) : Sets the UI object which implements the Look and feel for this component.  setSnapToTicks(boolean b): if true is passed then the slider position is placed to nearest tick.  setModel(BoundedRangeModel n) : sets the BoundedRangeModel that handles the slider’s three fundamental properties: minimum, maximum, value.  setLabelTable(Dictionary l) : Used to specify what label will be drawn at any given value.  setInverted(boolean b): if passed true then the slider is set to inverted.  imageUpdate(Image img, int s, int x, int y, int w, int h): repaints the component when the image has changed.  setExtent(int extent) : sets the size of the range “covered” by the knob.  removeChangeListener(ChangeListener l): removes a ChangeListener from the slider.  getModel(): returns the BoundedRangeModel that handles the slider’s three fundamental properties: minimum, maximum, value.  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.  getUI(): gets the UI object which implements the L&F for this component.  getPaintTrack() : returns if the tracks are painted or not.  getPaintTicks() : returns if ticks are painted or not  getPaintLabels() : returns whether labels are painted or not  getOrientation() : returns the orientation of the component.  getMinorTickSpacing() : returns the minor tick spacing  getMinimum(): returns the minimum value  getMaximum(): returns the maximum value  getMajorTickSpacing() : returns the major tick spacing.  addChangeListener(ChangeListener l) :dds a ChangeListener to the slider.\  createChangeListener() : create a change listener for the componentsetUI(SliderUI ui) : sets the look and feel object that renders this component.  getUI() : returns the look and feel object that renders this component.  paramString() : returns a string representation of this JSlider.  getUIClassID() : returns the name of the Look and feel class that renders this component.  getAccessibleContext() : gets the AccessibleContext associated with this JSlider.  
setOrientation(int n) : sets the orientation of the slider to the specified value  
setValue( int v) : sets the value of the slider to the specified value  
setPaintTicks(boolean b) : the boolean value determines whether the ticks are painted on the slider or not  
setPaintTrack(boolean b) : the boolean value determines whether the track are painted on the slider or not  
setMajorTickSpacing(int n) : sets the spacing for major ticks .  
setMinorTickSpacing(int n) : sets the spacing for minor ticks .  
setFont(Font f) : set the font of the text for the slider  
setMaximum(int m) : set the maximum value for the slider  
setMinimum(int m) : sets the minimum value for the slider  
updateUI() : Resets the UI property to a value from the current look and feel.  
setValueIsAdjusting(boolean b) : Sets the model’s valueIsAdjusting property to boolean b.  
setUI(SliderUI ui) : Sets the UI object which implements the Look and feel for this component.  
setSnapToTicks(boolean b): if true is passed then the slider position is placed to nearest tick.  
setModel(BoundedRangeModel n) : sets the BoundedRangeModel that handles the slider’s three fundamental properties: minimum, maximum, value.  
setLabelTable(Dictionary l) : Used to specify what label will be drawn at any given value.  
setInverted(boolean b): if passed true then the slider is set to inverted.  
imageUpdate(Image img, int s, int x, int y, int w, int h): repaints the component when the image has changed.  
setExtent(int extent) : sets the size of the range “covered” by the knob.  
removeChangeListener(ChangeListener l): removes a ChangeListener from the slider.  
getModel(): returns the BoundedRangeModel that handles the slider’s three fundamental properties: minimum, maximum, value.  
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.  
getUI(): gets the UI object which implements the L&F for this component.  
getPaintTrack() : returns if the tracks are painted or not.  
getPaintTicks() : returns if ticks are painted or not  
getPaintLabels() : returns whether labels are painted or not  
getOrientation() : returns the orientation of the component.  
getMinorTickSpacing() : returns the minor tick spacing  
getMinimum(): returns the minimum value  
getMaximum(): returns the maximum value  
getMajorTickSpacing() : returns the major tick spacing.  
addChangeListener(ChangeListener l) :dds a ChangeListener to the slider.\  
createChangeListener() : create a change listener for the component
setUI(SliderUI ui) : sets the look and feel object that renders this component.  
getUI() : returns the look and feel object that renders this component.  
paramString() : returns a string representation of this JSlider.  
getUIClassID() : returns the name of the Look and feel class that renders this component.  
getAccessibleContext() : gets the AccessibleContext associated with this JSlider.  
The Following programs will illustrate the use of JSlider1. program to create a simple JSlider  
Java
// java Program to create a simple JSliderimport javax.swing.event.*;import java.awt.*;import javax.swing.*;class solve extends JFrame {     // frame    static JFrame f;     // slider    static JSlider b;     // main class    public static void main(String[] args)    {        // create a new frame        f = new JFrame("frame");         // create a object        solve s = new solve();         // create a panel        JPanel p = new JPanel();         // create a slider        b = new JSlider();         // add slider to panel        p.add(b);         f.add(p);         // set the size of frame        f.setSize(300, 300);         f.show();    }}
Output :  
2 . Program to create a slider with min and max value and major and minor ticks painted.  
Java
// java Program to create a slider with min and// max value and major and minor ticks painted.import javax.swing.event.*;import java.awt.*;import javax.swing.*;class solve extends JFrame implements ChangeListener {     // frame    static JFrame f;     // slider    static JSlider b;     // label    static JLabel l;     // main class    public static void main(String[] args)    {        // create a new frame        f = new JFrame("frame");         // create a object        solve s = new solve();         // create label        l = new JLabel();         // create a panel        JPanel p = new JPanel();         // create a slider        b = new JSlider(0, 200, 120);         // paint the ticks and tracks        b.setPaintTrack(true);        b.setPaintTicks(true);        b.setPaintLabels(true);         // set spacing        b.setMajorTickSpacing(50);        b.setMinorTickSpacing(5);         // setChangeListener        b.addChangeListener(s);         // add slider to panel        p.add(b);        p.add(l);         f.add(p);         // set the text of label        l.setText("value of Slider is =" + b.getValue());         // set the size of frame        f.setSize(300, 300);         f.show();    }     // if JSlider value is changed    public void stateChanged(ChangeEvent e)    {        l.setText("value of Slider is =" + b.getValue());    }}
Output :  
3 . Program to create a vertical slider with min and max value and major and minor ticks painted and set the font of the slider.  
Java
// java Program to create a vertical slider with// min and max value and major and minor ticks// painted and set the font of the slider.import javax.swing.event.*;import java.awt.*;import javax.swing.*;class solve extends JFrame implements ChangeListener {     // frame    static JFrame f;     // slider    static JSlider b;     // label    static JLabel l;     // main class    public static void main(String[] args)    {        // create a new frame        f = new JFrame("frame");         // create a object        solve s = new solve();         // create label        l = new JLabel();         // create a panel        JPanel p = new JPanel();         // create a slider        b = new JSlider(0, 200, 120);         // paint the ticks and tracks        b.setPaintTrack(true);        b.setPaintTicks(true);        b.setPaintLabels(true);         // set spacing        b.setMajorTickSpacing(50);        b.setMinorTickSpacing(5);         // setChangeListener        b.addChangeListener(s);         // set orientation of slider        b.setOrientation(SwingConstants.VERTICAL);         // set Font for the slider        b.setFont(new Font("Serif", Font.ITALIC, 20));         // add slider to panel        p.add(b);        p.add(l);         f.add(p);         // set the text of label        l.setText("value of Slider is =" + b.getValue());         // set the size of frame        f.setSize(300, 300);         f.show();    }     // if JSlider value is changed    public void stateChanged(ChangeEvent e)    {        l.setText("value of Slider is =" + b.getValue());    }}
Output :  
Note : The above programs might not run in an online compiler please use an offline IDE 
ManasChhabra2
sweetyty
java-swing
Java
Programming Language
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.