🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this post, I show you how to create a slider using the JSlider component in swing-based applications.
Check out the complete Swing tutorial at https://www.javaguides.net/p/java-swing-tutorial.html.
JSlider is a component that lets the user graphically select a value by sliding a knob within a bounded interval. Moving the slider's knob, the stateChanged() method of the slider's ChangeListener is called.
Tick marks - JSlider can optionally show tick marks for the range of its values. The tick marks are controlled with the setMinorTickSpacing(), setMajorTickSpacing(), and setPaintTicks() methods.
Java Swing Slider Example
In the below code example, a value selected from a slider is displayed in a label component:
import javax.swing.GroupLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import java.awt.EventQueue;
public class SliderExample extends JFrame {
private static final long serialVersionUID = 1L;
private JSlider slider;
private JLabel lbl;
private void initializeUI() {
slider = new JSlider(0, 100, 0);
slider.setMinorTickSpacing(5);
slider.setMajorTickSpacing(10);
slider.setPaintTicks(true);
slider.addChangeListener((ChangeEvent event) -> {
int value = slider.getValue();
lbl.setText(Integer.toString(value));
});
lbl = new JLabel("...");
createLayout(slider, lbl);
setSize(300, 150);
setTitle("JSlider");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void createLayout(JComponent...arg) {
JPanel pane = (JPanel) getContentPane();
GroupLayout gl = new GroupLayout(pane);
pane.setLayout(gl);
gl.setAutoCreateContainerGaps(true);
gl.setAutoCreateGaps(true);
gl.setHorizontalGroup(gl.createParallelGroup()
.addComponent(arg[0])
.addComponent(arg[1])
);
gl.setVerticalGroup(gl.createSequentialGroup()
.addComponent(arg[0])
.addComponent(arg[1])
);
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
SliderExample sliderExample = new SliderExample();
sliderExample.initializeUI();
sliderExample.setVisible(true);
});
}
}
Let's understand the above Java program.
A JSlider is created with the minimum, maximum, and current values are parameters:
slider = new JSlider(0, 100, 0);
We set the distances between minor a major tick marks:
slider.setMinorTickSpacing(5);
slider.setMajorTickSpacing(10);
The setPaintTicks() method determines whether tick marks are painted on the slider:
slider.setPaintTicks(true);
Output
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Hello Ramesh,
ReplyDeletebasically some good solution, but if you put a source here, it should throw no errors, when copied to some extra made test class (got more than ten errors !)
(Its from a space between "-" and ">")
Yeah, there is format error. Fixed it , thanks for reporting.
Delete