Set the JSlider vertical and move bottom-to-top in Java


To set the JSlider vertical, use the VERTICAL constant while creating the slider. Let us create a new Slider −

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

The other parameter values set above allows you to include the minimum, maximum and the initial value of the slider.

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

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(false);
      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

Updated on: 30-Jul-2019

76 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements