How to hide the track on the slider in Java?

Java 8Object Oriented ProgrammingProgramming

To hide the track on the slider, you need to use the setPaintTrack() method and set it to FALSE −

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

The above method setPaintTrack() is by default set to TRUE.

The following is an example to hide the track on the slider −

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.blue);
      slider.setForeground(Color.white);
      slider.setPaintTrack(false);
      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 09:30:56
Advertisements