Updated July 7, 2023
Introduction to JSlider
It is a class in Java, which extends JComponent and implements SwingConstants. The main purpose of JSlider is to let users adjust a numeric value between minimum and maximum values. It has four important aspects:
- Current Value,
- Minimum Value,
- Maximum value,
- And an Orientation.
Current, Maximum and Minimum values define the common highest and the lowest value, and the Orientation defines whether the slider is horizontal or a vertical one. For Orientation, SwingConstants.VERTICAL and SwingConstants.HORIZONTAL is used. getValue() method is used for Current Value and setValue() method to set a current value. Along with Slider and Knot, we can display a Label with values like Poor, Average and Excellent. It comes with standards labels, which are placed using JLabel Component. Besides Standard Label, It supports custom labels.
Now that we have understood JSlider, let’s move on to the list of constructors it has. Upon creation of an object of a class, a constructor is called. A constructor may or may not take any parameters.
Constructors in JSlider
Like any other class in JAVA, it has a collection of Constructors. Following are the constructors in JSlider with their respective descriptions:
- JSlider(): It is one of the basic Constructors with a horizontal slider with an initial value of 50 and a value range from 0 to 100.
- JSlider(int orientation): Includes a specification for the slider to be horizontal or vertical, as mentioned above, using SwingConstants.VERTICAL and SwingConstants.HORIZONTAL Slider’s position can be fixed. Value ranges similar to JSlider().
- JSlider(int min, int max): Similar to the above-mentioned Slider, but along with the minimum and maximum values and an initial value which is the average of the minimum and maximum value specified.
- JSlider(int min, int max, int value): Creates a simple horizontal slider, as explained above, with the specified minimum, maximum and current values.
- JSlider(int orientation, int min, int max, int value): This constructor simply includes every aspect of it. This method creates a horizontal slider with a specified orientation and specified minimum, maximum values, and current value. This constructor, compared to others, takes up to four parameters.
Other than these constructors, JSlider provides a few more constructors for specific implementations. Now, let’s look at the methods from the JSlider class.
Methods
A java method is a collection of code statements, similar to the above-mentioned constructor, with a name specified and can be called/invoked anytime and anywhere in the code. A method can be seen as a subprogram. Unlike a constructor, a method returns a value.
Now, moving further, its class provides a range of methods, below are the few methods with details:
- public int getMajorTickSpacing() and public void setMajorTickSpacing(int n) are two of the basic methods that are used to set the major tick spacing for the slider and also to returns the current set value, which represents the distance between each tick mark.
- public int getMinorTickSpacing() and public void setMinorTickSpacing(int n), similar to above, returns the major tick spacing and sets the major tick spacing.
- public boolean getPaintTicks() returns true if the tick mark is painted, false if otherwise.
- public void setPaintTicks(boolean b) is by default false and decides if the tick mark is to be painted on the slider.
- public boolean getPaintLabels() simply tell if the label is to be painted. True if the label is to be painted, else false.
- public void setPaintLabels(boolean b) decides if the label is to be painted, by default it is false. This method sets a Label Table, which is then set on the slider and called using setLabelTable.
Above mentioned methods are basic and Java’s JSlider provides a wide range of methods for various operations.
Program to Implement JSlider
Now that we have understood JSlider class along with its methods and constructors, let’s check out the implementation of it with a sample program.
Code:
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;public class jslider_sample extends JPanel {
public jslider_sample() {
super(true);
this.setLayout(new BorderLayout());
JSlider sample_slider = new JSlider(JSlider.HORIZONTAL, 0, 50, 25);
sample_slider.setMinorTickSpacing(2);
sample_slider.setMajorTickSpacing(10);
sample_slider.setPaintTicks(true);
sample_slider.setPaintLabels(true);
sample_slider.setLabelTable(sample_slider.createStandardLabels(10));
add(sample_slider, BorderLayout.CENTER);
}
public static void main(String s[]) {
JFrame example_frame = new JFrame("Slider Example");
example_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
example_frame.setContentPane(new jslider_sample());
example_frame.pack();
example_frame.setVisible(true);
}
}
Output:
Save the above code in a file with .java extension. Compile the java code with javac filename.java and then execute the class file as a java class. Upon executing the code you’ll have a JSlide Example Box with a Slider and a Knot. A slider will have a range of numbers for indication. Refer below screenshot for further understanding.
Code Interpretation
Basically, we did import two major components namely AWT (Abstract Window Toolkit) and SWING. We then created a new class with the name jslider_sample which inherits JPanel properties. With JSlider we constructed a new JSlider() constructor with JSlider.HORIZONTAL to specify the position, 0,50,25 to indicate the minimum, maximum and current values. As seen in the above screenshot, the minimum value is 0, the maximum value is 50 and the current knot value is 25 as specified.
Later we defined setMinorTickSpacing and setMajorTickSpacing with the values like 2 for minor and 10 for major tick spacing. And the values for Paint Ticks and Paint Labels are true. Now, in the main class, we began with naming the frame as “Slider Example” then we set jslider_sample inside the frame with frame.setContentPane(new jslider_sample());
In the next line of code, the pack method maintains the frame with all its content in preferred sizes. The use of pack() is always recommended. setVisible(boolean b) is the deciding factor. It decides if the window is to be displayed or hidden depending on the parameter provided, in our case, we have it set to true.
Conclusion
To conclude, JSlide is a Java Class that comes with a slider, along with a knot to indicate the value. We understood a few of the basic methods and constructors used in it along with its class itself. With the program example, we implemented a simple Slider with the minimum, maximum and current values, and Labels as value indicators.
Recommended Articles
This is a guide to JSlider. Here we discuss the basic concept, constructors, methods, program to implement JSlider with example and Code Interpretation. You may also look at the following articles to learn more –