TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java Swing

Create a Slider using JSlider Class

Overview

In this tutorial, we will learn the steps to create a Slider using the Swing JSlider Class. A slider is a GUI component that allows users to set values by moving an indicator in a vertical or horizontal fashion. Users can also click on points to change the current value.

Swing JSlider Class

We can create a slider with two types of tick marks: minor and major. The minor ticket is shorter than the major tick mark. To create a slider we use the JSlider class.

To import the class we can use the following statement:

import javax.swing.JSlider;

To create a horizontal slider, we can use the following code:

JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 40);

Java Program

package com.testingdocs.swing.components;

import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JSlider;

public class JSliderDemo {

 public static void main(String[] args) {
 JFrame frame = new JFrame("JSlider - www.TestingDocs.com");

 // Create a Slider
 JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 40);
 slider.setMinorTickSpacing(10);
 slider.setMajorTickSpacing(20);
 slider.setPaintTicks(true);
 slider.setPaintLabels(true);
 slider.setLabelTable(slider.createStandardLabels(20));

 // Frame Settings
 frame.setLayout(new FlowLayout());
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(500, 400);
 frame.getContentPane().add(slider);
 frame.setVisible(true);
 }
}

Output

Run the program to view the output.

JSlider Swing Class

 

—

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/in/java/

Related Posts

Add GUI Components Swing

Java Swing /

Add GUI Components to a Swing Container

JPanel Container Class

Java Swing /

Swing JPanel Container Class

Swing CardLayout

Java Swing /

Java Swing CardLayout

Java Swing GridLayout

Java Swing /

Java Swing GridLayout

Java Swing FlowLayout

Java Swing /

Java Swing FlowLayout

‹ Create Progress Bar using JProgressBar› Java Swing JFileChooser

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version