Create Slider in Java:
We can use the Eclipse IDE for this example. If you do not know about it then follow this link- How to Install Eclipse For Java and create a program in it. |
A slider is a component that allows the user to adjust a number graphically within a range of values. Sliders, which are created from the JSlider class, display an image of a “slider knob” that can be dragged along the track. Sliders can be horizontally or vertically oriented. A slider is designed to represent a range of numeric values. At one end of the slider is the range’s minimum value and at the other end is the range’s maximum value.
Java Slider Constructors:
Constructor | Description |
---|---|
JSlider( ) | Creates a slider with an initial value of 50 and a range of 0 to 100. |
JSlider(int orientation) | Creates a slider with the specified orientation set by either JSlider.HORIZONTAL or JSlider.VERTICAL with the range 0 to 100 and initial value 50. |
JSlider(int min, int max) | Creates a horizontal slider using the given min and max. with an initial value equal to the average of the min plus max. |
JSlider(int min, int max, int value) | Creates a horizontal slider using the given min, max, and value. |
JSlider(int orientation, int min, int max, int value) | Creates a slider with the specified orientation and the specified minimum, maximum, and initial values. |
JSlider(BoundedRangeModel brm) | Creates a horizontal slider using the specified BoundedRangeModel. |
Java Slider Methods:
Methods | Description |
---|---|
setMinorTickSpacing(int n) | This method sets the minor tick spacing to the slider. |
setMajorTickSpacing(int n) | This method sets the major tick spacing to the slider. |
setPaintTicks(boolean b) | This method is used to determine whether tick marks are painted on the slider. |
setPaintLabels(boolean b) | This method is used to determine whether labels are painted on the slider. |
setPaintTrack(boolean b) | This method is used to determine whether the track is painted on the slider. |
setValue(int n) | Sets the sliders current value. |
setOrientation(int orientation) | Set the orientation of the scrollbar to either VERTICAL or HORIZONTAL. |
Sample Code to Create Slider in Java:


Output:

import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.event.*;
public class Slider implements ChangeListener{
JFrame frame;
JPanel panel;
JLabel label;
JSlider j_slider;
Slider(){
frame = new JFrame("Slider");
panel = new JPanel();
label = new JLabel();
j_slider = new JSlider(0, 100, 50);
j_slider.setPreferredSize(new Dimension(450, 225));
j_slider.setPaintTicks(true);
j_slider.setMinorTickSpacing(10);
j_slider.setPaintTrack(true);
j_slider.setMajorTickSpacing(25);
j_slider.setPaintLabels(true);
j_slider.setFont(new Font("Open Sans",Font.PLAIN,18));
label.setFont(new Font("Open Sans",Font.PLAIN,18));
j_slider.setOrientation(SwingConstants.VERTICAL);
// j_slider.setOrientation(SwingConstants.HORIZONTAL);
label.setText("Temperature = "+ j_slider.getValue() + " °C");
j_slider.addChangeListener(this);
panel.add(j_slider);
panel.add(label);
frame.add(panel);
frame.setSize(500, 300);
frame.setVisible(true);
}
@Override
public void stateChanged(ChangeEvent e) {
label.setText("Temperature = "+ j_slider.getValue() + " °C");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Slider slider = new Slider();
}
}
Comments (No)