Java Swing How to - Create a slider with JSlider








Question

We would like to know how to create a slider with JSlider.

Answer

/*from w  ww .j  a v  a2 s .  c  o m*/
import javax.swing.JSlider;

public class Main {
  public static void main(String[] argv) throws Exception {
    // Create a horizontal slider with min=0, max=100, value=50
    JSlider slider = new JSlider();

    // Create a horizontal slider with custom min and max; value is set to the middle
    int minimum = -255;
    int maximum = 256;
    slider = new JSlider(minimum, maximum);
  }
}