How to move the horizontal slider right-to-left in Java?

Java 8Object Oriented ProgrammingProgramming

At first, let us create a horizontal slider −

JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 55);

Now, we will set it to move right-to-left using setInverted() −

slider.setInverted(true);

The following is an example to move the horizontal slider right-to-left −

Example

package my;
import java.awt.Color;
import java.awt.Font;
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.HORIZONTAL, 0, 100, 55);
      slider.setInverted(true);
      slider.setMinorTickSpacing(10);
      slider.setMajorTickSpacing(25);
      slider.setPaintTicks(true);
      slider.setPaintLabels(true);
      slider.setBackground(Color.orange);
      slider.setForeground(Color.white);
      Font font = new Font("Serif", Font.BOLD, 20);
      slider.setFont(font);
      JPanel panel = new JPanel();
      panel.add(slider);
      frame.add(panel);
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      frame.setSize(600, 300);
      frame.setVisible(true);
   }
}

Output


raja
Published on 04-Jun-2019 12:53:50
Advertisements