CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Swing > JSlider in Java Swing

Swing Tutorial

  • What Is Java Swing
  • First Program
  • What is AWT
  • Advantages of Swing Over AWT
  • JFrame
  • Swing Components
  • JLabel
  • JTextField
  • JPasswordField
  • JButton
  • Event Handling
  • FlowLayout
  • BorderLayout
  • GridLayout
  • GridBagLayout
  • BoxLayout
  • JPanel
  • JCheckBox
  • JRadioButton
  • Border Component
  • JComboBox
  • JList
  • JList with MVC
  • Mouse Event
  • Key Event
  • JMenuBar
  • JTextArea
  • Using Color
  • Use Font
  • Display Image/Icon
  • Dialog Box
  • JTable
  • JDesktopPane and JInternalFrame
  • JDesktopPane
  • JInternalFrame
  • Adapter Classes
  • Swing Timer
  • JScrollPane
  • JSlider
  • JProgressBar
  • JSeparator
  • JToolBar
  • ActionListener
  • ItemListener
  • Worker Thread
  • Nimbus Look and Feel

JSlider in Java Swing

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: August 27, 2021 · 1 min read · 0 Comments
Share: in X

Following is a simple example of how we can create and use a JSlider in Java Swing to control a value. In this example, we’ll create a GUI application that has a slider and a label to display the current value selected by the slider:

import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class JSliderDemo {
	public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            createAndShowGUI();
        });
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("JSlider Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 150);
        frame.setLayout(new FlowLayout());

        // Create a label to display the slider value
        JLabel valueLabel = new JLabel("Selected Value: 0");
        frame.add(valueLabel);

        // Create a slider
        JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);
        slider.setMajorTickSpacing(20);
        slider.setMinorTickSpacing(5);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);

        // Add a change listener to the slider
        slider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                int selectedValue = slider.getValue();
                valueLabel.setText("Selected Value: " + selectedValue);
            }
        });

        frame.add(slider);
        frame.setVisible(true);
    }
}

Output:

jslider in java swing

Related Posts:

  • Control Statements in Java
  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
  • Event Handling in Java Swing
  • JComboBox in Java Swing
  • ItemListener in Java
  • Java Swing Tutorial
Tags:guijavaswing
Was this article helpful?
← Previous ArticleJScrollPane in Java
Next Article →JProgressBar in Java Swing

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap