- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a horizontal slider with custom min, max, and initial value in Java
While creating horizontal slider, we can set custom values as well. Let us take three integer variable and set the int values for min, max as well as the initial value of the slider −
int val = 75; int min = 0; int max = 100;
Set it to the slider while creating a new slider. Here, we have set the constant to be HORIZONTAL, since we are creating a horizontal slider −
JSlider slider = new JSlider(JSlider.HORIZONTAL,min, max, val);
The following is an example to create a horizontal slider with custom min, max and initial value −
Example
package my; import java.awt.Color; import java.awt.Font; 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"); int val = 75; int min = 0; int max = 100; JSlider slider = new JSlider(JSlider.HORIZONTAL,min, max, val); slider.setMinorTickSpacing(10); slider.setMajorTickSpacing(25); slider.setPaintTicks(true); slider.setPaintLabels(true); slider.setBackground(Color.MAGENTA); slider.setForeground(Color.black); slider.setSnapToTicks(true); System.out.println("Snapping to tick marks? = "+slider.getSnapToTicks()); Font font = new Font("Serif", Font.BOLD, 13); slider.setFont(font); JPanel panel = new JPanel(); panel.add(slider); frame.add(panel); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setSize(600, 300); frame.setVisible(true); } }
This will produce the following output −
- Related Articles
- Create a vertical slider with custom min, max, and initial value in Java
- How to create Horizontal Slider in Java?
- How to define a MIN and MAX value for EditText in Android?
- Sorting max to min value in MySQL
- How to find Min/Max numbers in a java array?
- How to move the horizontal slider right-to-left in Java?
- How to define a MIN and MAX value for EditText in Android using Kotlin?
- Java Program to generate random number array within a range and get min and max value
- How to SELECT min and max value from the part of a table in MySQL?
- Minimum value of “max + min” in a subarray in C++
- How to use min and max attributes in HTML?
- max() and min() in Python
- How to create a JavaFX slider?
- How to create JavaFX slider with two thumbs?
- Python - Assign a specific value to Non Max-Min elements in Tuple

Advertisements