Java Swing How to - Compare the tick settings in JSlider








Question

We would like to know how to compare the tick settings in JSlider.

Answer

//from   w  w  w. j a  v  a  2  s  .  co  m
import java.awt.BorderLayout;
import java.awt.Container;

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

public class Main {
  public static void main(String args[]) {
    JFrame f = new JFrame("Tick Slider");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // No Ticks
    JSlider jSliderOne = new JSlider();
    // Major Tick 25 - Minor 5
    JSlider jSliderTwo = new JSlider();
    jSliderTwo.setMinorTickSpacing(5);
    jSliderTwo.setMajorTickSpacing(25);
    jSliderTwo.setPaintTicks(true);
    jSliderTwo.setSnapToTicks(true);
    // Major Tick 25 - Minor 6
    JSlider jSliderThree = new JSlider(JSlider.VERTICAL);
    jSliderThree.setMinorTickSpacing(6);
    jSliderThree.setMajorTickSpacing(25);
    jSliderThree.setPaintTicks(true);
    JSlider jSliderFour = new JSlider(JSlider.VERTICAL);
    // Major Tick 25 - Minor 1
    jSliderFour.setMinorTickSpacing(1);
    jSliderFour.setMajorTickSpacing(25);
    jSliderFour.setPaintTicks(true);

    Container c = f.getContentPane();
    c.add(jSliderOne, BorderLayout.NORTH);
    c.add(jSliderTwo, BorderLayout.SOUTH);
    c.add(jSliderThree, BorderLayout.WEST);
    c.add(jSliderFour, BorderLayout.EAST);
    f.setSize(300, 200);
    f.setVisible(true);
  }
}