Introduction
This article exaplains 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 it's 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:
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): develops 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.
import javax.swing.*;
public class JsliderEx extends JFrame
{
public JsliderEx()
{
JSlider slider1 = new JSlider(JSlider.HORIZONTAL, 0, 100, 25);
JPanel panel1=new JPanel();
panel1.add(slider1);
add(panel1);
}
public static void main(String s[])
{
JsliderEx frm = new JsliderEx();
frm.pack();
frm.setVisible(true);
}
}
Output
2. Example
In this example we modify our previous program, now we develop a slider with painted ticks.
import javax.swing.*;
public class JsliderEx2 extends JFrame
{
public JsliderEx2()
{
JSlider slider12 = new JSlider(JSlider.HORIZONTAL, 0, 50, 25);
slider12.setMinorTickSpacing(2);
slider12.setMajorTickSpacing(10);
slider12.setPaintLabels(true);
slider12.setPaintTicks(true);
JPanel panel1=new JPanel();
panel1.add(slider12);
add(panel1);
}
public static void main(String s[])
{
JsliderEx2 frm=new JsliderEx2();
frm.pack();
frm.setVisible(true);
}
}
Output
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.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class SliderChangeAction
{
JSlider sliderobj;
JLabel labelobj;
public static void main(String[] args)
{
SliderChangeAction cactn = new SliderChangeAction();
}
public SliderChangeAction()
{
JFrame frm = new JFrame("Slider Frame");
sliderobj = new JSlider();
sliderobj.setValue(70);
sliderobj.addChangeListener(new ChangeAction());
labelobj = new JLabel("C-Sharpcorner.com");
JPanel panelobj = new JPanel();
panelobj.add(sliderobj);
panelobj.add(labelobj);
frm.add(panelobj, BorderLayout.CENTER);
frm.setSize(300, 300);
frm.setVisible(true);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class ChangeAction implements ChangeListener
{
public void stateChanged(ChangeEvent ch)
{
int value = sliderobj.getValue();
String string = Integer.toString(value);
labelobj.setText(string);
}
}
}
Output
When we click on the slider to select a value the "changeEvent" is performed and now the slider shows the slected value istead of c-sharpcorner.com.