
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to set the extent in JSlider
In this article, we will learn how to set the extent of a JSlider using Java Swing. The extent defines the size of the range that the slider's knob can move within, restricting its movement past a certain point.
JSlider
JSlider is a component in Java Swing that provides a visual way to select a value by sliding a knob within a specified range. The setExtent() method in JSlider sets the size of the range that the knob covers, which controls how far it can move.
setExtent() method
The setExtent() method in Java is used to:
- Set the size of the visible area in components like Scrollbar.
- Control how much content can be viewed at once within the scrollable area.
Steps to set the extent in JSlider
The following are the steps to set the extent in JSlider ?
-
Step 1. Create a JSlider Object: We first create a JSlider object with horizontal orientation and set its range from 0 to 100, with an initial value of 70. Here, the slider starts at 70 within the range of 0 to 100.
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 70);
-
Step 2. Configure Tick Spacing and Labels: We set minor tick spacing of 5 and major tick spacing of 20 to display small and large intervals on the slider. We also enable tick marks and labels. This will create ticks and labels at regular intervals to guide the user.
slider.setMinorTickSpacing(5);
slider.setMajorTickSpacing(20);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
-
Step 3. Set the Extent of the Slider: We set the extent of the slider to 20, limiting how far the knob can be moved. This restricts the slider from extending past the set extent.
slider.setExtent(20);
-
Step 4. Add Slider to a Panel and Display the Frame: We add the slider to a JPanel, then add the panel to a JFrame. The frame is made visible with a specific size. This displays the slider within a window, ready for interaction.
JPanel panel = new JPanel();
panel.add(slider);
frame.add(panel);
frame.setSize(500, 300);
frame.setVisible(true);
Example
The following is an example to set the extent in JSlider ?
package my; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.WindowConstants; public class SwingDemo { public static void main(String[] args) { JFrame frame = new JFrame("Frame with Slider"); JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 70); slider.setMinorTickSpacing(5); slider.setMajorTickSpacing(20); slider.setPaintTicks(true); slider.setPaintLabels(true); slider.setExtent(20); JPanel panel = new JPanel(); panel.add(slider); frame.add(panel); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setSize(500, 300); frame.setVisible(true); } }
The output is as follows displaying the JSlide ?
Now you can't extend the JSlider to the end since you have set the extent above ?
Advertisements