Java Program to set the extent in JSlider

JavaObject Oriented ProgrammingProgramming

To set the extent of the slider, use the setExtent() method. It sets the size of the range covered by the knob −

JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 70);
slider.setMinorTickSpacing(5);
slider.setMajorTickSpacing(20);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.setExtent(20);

The following is an example to set the extent in JSlider −

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.HORIZONTAL, 0, 100, 70);
      slider.setMinorTickSpacing(5);
      slider.setMajorTickSpacing(20);
      slider.setPaintTicks(true);
      slider.setPaintLabels(true);
      slider.setExtent(20);
      JPanel panel = new JPanel();
      panel.add(slider);
      frame.add(panel);
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      frame.setSize(500, 300);
      frame.setVisible(true);
   }
}

The output is as follows displaying the JSlide −

Now you can’t extend the JSlider to the end since you have set the extent above −


raja
Published on 03-Jun-2019 08:58:46
Advertisements