Working Of JSlider Class Of Swing In Java

This article exaplains how the JSlider class of Swing in Java works.

Introduction

 
This article explains how the JSlider class of Swing in Java works.
 

JSlider in Swing

 
JSlider constructs/designs a slider. By using JSlider a user can select a value from a range. To create the slider class, create an instance using its constructor JSlider().
 

What is slider?

 
A slider provides a way for the user to enter a numeric value more easily since a slider is bounded by a minimum and maximum value, the user must choose from within that range.
 
The following figure clarifies what a slider is:
pic-2.jpg
Some common constructors used in JSlider are:
  • JSlider(): creates a slider with default initial value 50 and range 0-100.
  • JSlider( int operation): develops a slider with the specified orientation set by the user.
  • JSlider( int min, int max, int values): creates a horizontal slider using the given min and max.
  • JSlider( int min, int max): develops a horizontal slider with specified min, max, and value.
  • JSlider( int orientation, int min, int max, int value): develop a slider with specified orientation, that must be either JSlider.HORIZONTAL or JSlider.VERTICAL.
Some common public methods used in JSlider are:
  • void setMinorTickSpacing(int p):  sets the minor tick spacing in the slider.
  • void SetMajorTickSpacing (int p): sets the major tick spacing.
  • void setMinimum(int p): sets the minimum value of the slider.
  • void setMaximum(int p): sets the maximum value of the slider.
  • void setPaintsTicks(boolean bl): determines that tick mark is painted.
  • void setPaintLabels(boolean bl); tests whether labels are painted.
  • void setPaintTracks(boolean bl); determines whether the track is painted.
1. Example
 
In this example, we create a simple slider using the JSlider class.
  1. import javax.swing.*;  
  2. public class JsliderEx extends JFrame {  
  3.  public JsliderEx() {  
  4.   JSlider slider1 = new JSlider(JSlider.HORIZONTAL, 010025);  
  5.   JPanel panel1 = new JPanel();  
  6.   panel1.add(slider1);  
  7.   add(panel1);  
  8.  }  
  9.  public static void main(String s[]) {  
  10.   JsliderEx frm = new JsliderEx();  
  11.   frm.pack();  
  12.   frm.setVisible(true);  
  13.  }  
  14. }   
Output
 
pic-3.jpg
 
2. Example 
 
In this example we modify our previous program, now we develop a slider with painted ticks.
  1. import javax.swing.*;  
  2. public class JsliderEx2 extends JFrame {  
  3.  public JsliderEx2() {  
  4.   JSlider slider12 = new JSlider(JSlider.HORIZONTAL, 05025);  
  5.   slider12.setMinorTickSpacing(2);  
  6.   slider12.setMajorTickSpacing(10);  
  7.   slider12.setPaintLabels(true);  
  8.   slider12.setPaintTicks(true);  
  9.   JPanel panel1 = new JPanel();  
  10.   panel1.add(slider12);  
  11.   add(panel1);  
  12.  }  
  13.  public static void main(String s[]) {  
  14.   JsliderEx2 frm = new JsliderEx2();  
  15.   frm.pack();  
  16.   frm.setVisible(true);  
  17.  }  
  18. }   
Output
 
pic-4.jpg
 
Some JSlider terms used in our next example. In this example we see the event change in our slider program, for changing event we use the "ChangeListener" interface, "ChangeEvent" class and "addChangeListener" object.
 
JSliderChangeListener interface
 
The Change Listener is in the interface category that is used to change the state when we call the stateChanged() method that receives the event generated by the slider using addChangeListener() method of the JSlider class.
 

JSlider ChangeEvent class

 
ChangeEvent is a class that handles the event developed by components of the JSlider on a change of state.
 
JSlider addChangeListener() object
 
addChangeListener is a method of the JSlider class, used to handle an event of a change of the selected state of the JSlider component.
 
Example
 
In this example, we create a slider with additional features, like State change, Change event, etcetera. For changing the state we use the stateChanged() method.
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import javax.swing.*;  
  4. import javax.swing.event.*;  
  5. public class SliderChangeAction {  
  6.  JSlider sliderobj;  
  7.  JLabel labelobj;  
  8.  public static void main(String[] args) {  
  9.   SliderChangeAction cactn = new SliderChangeAction();  
  10.  }  
  11.  public SliderChangeAction() {  
  12.   JFrame frm = new JFrame("Slider Frame");  
  13.   sliderobj = new JSlider();  
  14.   sliderobj.setValue(70);  
  15.   sliderobj.addChangeListener(new ChangeAction());  
  16.   labelobj = new JLabel("C-Sharpcorner.com");  
  17.   JPanel panelobj = new JPanel();  
  18.   panelobj.add(sliderobj);  
  19.   panelobj.add(labelobj);  
  20.   frm.add(panelobj, BorderLayout.CENTER);  
  21.   frm.setSize(300300);  
  22.   frm.setVisible(true);  
  23.   frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  24.  }  
  25.  public class ChangeAction implements ChangeListener {  
  26.   public void stateChanged(ChangeEvent ch) {  
  27.    int value = sliderobj.getValue();  
  28.    String string = Integer.toString(value);  
  29.    labelobj.setText(string);  
  30.   }  
  31.  }  
  32. }   
Output
 
 pic-5.jpg
 
When we click on the slider to select a value the "changeEvent" is performed and now the slider shows the selected value instead of c-sharpcorner.com.
 
pic-6.jpg