Tutorialspoint

1 Answer
Ankith Reddy

To set the JSlider to be vertical, use the VERTICAL constant while creating the slider −

JSlider slider = new JSlider(JSlider.VERTICAL, 0, 100, 60);

Now, for the inverted slider i.e. moving from top-to-bottom −

slider.setInverted(true);

The following is an example to set the slider vertical and move top-to-bottom −

Example

package my;
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");
      JSlider slider = new JSlider(JSlider.VERTICAL, 0, 100, 60);
      slider.setInverted(true);
      slider.setMinorTickSpacing(5);
      slider.setMajorTickSpacing(20);
      slider.setPaintTicks(true);
      slider.setPaintLabels(true);
      JPanel panel = new JPanel();
      panel.add(slider);
      frame.add(panel);
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      frame.setSize(600, 300);
      frame.setVisible(true);
   }
}

Output

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.