
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to detect the value change of a JSlider in Java?
A JSlider is a subclass of JComponent class and it is similar to scroll bar which allows the user to select a numeric value from a specified range of integer values. A JSlider has a knob which can slide on a range of values and can be used to select a particular value. and it can generate a ChangeListener interface.
We can detect the value changed when the slider is moved horizontally using the Graphics2D class and override paint() method.
Example
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class ValueChangeJSliderTest extends JFrame { private JSlider slider; public ValueChangeJSliderTest() { setTitle("ValueChangeJSlider Test"); slider = new JSlider(JSlider.HORIZONTAL, 0, 20, 1); slider.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent ce) { repaint(); } }); setLayout(new BorderLayout()); slider.setBackground(Color.white); slider.setMajorTickSpacing(1); slider.setPaintTicks(true); slider.setPaintLabels(true); add(slider, BorderLayout.NORTH); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; g2d.setFont(new Font("Serif", Font.ITALIC, 25)); g2d.drawString(""+slider.getValue(), 200, 130); } public static void main(String[] args) { new ValueChangeJSliderTest(); } }
Output
- Related Articles
- How to change the maximum value of a JSlider in Java?
- How to change the minimum value of a JSlider in Java?
- How to change the position of a JSlider to horizontal/vertical programmatically in Java?
- Java Program to set the extent in JSlider
- How to create a JSlider that snap to the closest tick mark in Java?
- How to detect orientation change in layout in android?
- Display tick marks in a JSlider with Java
- How to determine whether the JSlider is currently snapping to tick marks in Java?
- How to detect orientation change in layout in Android using Kotlin?
- Set the JSlider vertical and move bottom-to-top in Java
- Make the JSlider vertical and move top-to-bottom in Java
- How to change the value of an attribute in javascript?
- Java Program to set major tick marks in a JSlider every 25 units
- Java Program to set minor tick marks in a JSlider every 10 units
- Java Program to Detect loop in a LinkedList

Advertisements