Java Swing How to - Set and get value from JSlider








Question

We would like to know how to set and get value from JSlider.

Answer

/*from  w  w w .j  a va  2 s .c om*/
import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JSlider;

public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JSlider oneJSlider = new JSlider();

    oneJSlider.setValue(27);
    
    int value = oneJSlider.getValue();
    System.out.println(value);
  
    oneJSlider.setPaintTicks(true);
    
    frame.add(oneJSlider, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}