JSlider in Java Swing

Slider in Swing is similar to scroll bar which allows the user to select a numeric value from a specified range of integer values.

 

It has a knob(point) which can slide on a range of values(minimum and maximum) and can be used to select a particular value.

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import javax.swing.event.*;

/*

<APPLET

   CODE=slider.class

   WIDTH=300

   HEIGHT=200 >

</APPLET>

*/

public class slider extends JApplet implements ActionListener, ChangeListener

{

   JSlider jslider = new JSlider(SwingConstants.HORIZONTAL, 0, 100, 0);

   JButton jbutton = new JButton("Set extent to 60");

   public void init()

   {

       Container contentPane = getContentPane();

       contentPane.setLayout(new FlowLayout());

       jslider.addChangeListener(this);

       jslider.putClientProperty("JSlider.isFilled", Boolean.TRUE);

       jslider.setPaintTicks(true);

       jslider.setPaintLabels(true);

       jslider.setMajorTickSpacing(20);

       jslider.setMinorTickSpacing(10);

       contentPane.add(jslider);

       jbutton.addActionListener(this);

       contentPane.add(jbutton);

   }

public void stateChanged(ChangeEvent changeevt)

   {

       JSlider jsliderr1 = (JSlider) changeevt.getSource();

       showStatus("Slider minimum: " + jsliderr1.getMinimum() +

           ", maximum: " + jsliderr1.getMaximum() +

           ", value: " + jsliderr1.getValue() +

           ", extent: " + jsliderr1.getExtent());

   }

public void actionPerformed(ActionEvent actevnt)

 {

       jslider.setExtent(60);

       jslider.revalidate();

   }

}

JSlider in Java Swing

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @