SWING - JSlider Class

The class JSlider is a component which lets the user graphically select a value by sliding a knob within a bounded interval.
Following is the declaration for javax.swing.JSlider class −
public class JSlider
   extends JComponent
      implements SwingConstants, Accessible

Following are the fields for javax.swing.JSlider class −
protected ChangeEvent changeEvent − Only one ChangeEvent is needed per slider instance since the event's only (read-only) state is the source property.
protected ChangeEvent changeEvent − Only one ChangeEvent is needed per slider instance since the event's only (read-only) state is the source property.
protected ChangeListener changeListener − The changeListener (no suffix) is the listener we add to the slider's model.
protected ChangeListener changeListener − The changeListener (no suffix) is the listener we add to the slider's model.
protected int majorTickSpacing − The number of values between the major tick marks -- the larger marks that break up the minor tick marks.
protected int majorTickSpacing − The number of values between the major tick marks -- the larger marks that break up the minor tick marks.
protected int minorTickSpacing − The number of values between the minor tick marks -- the smaller marks that occur between the major tick marks.
protected int minorTickSpacing − The number of values between the minor tick marks -- the smaller marks that occur between the major tick marks.
protected int orientation − Whether the slider is horizontal or vertical The default is horizontal.
protected int orientation − Whether the slider is horizontal or vertical The default is horizontal.
protected  BoundedRangeModel	sliderModel − The data model that handles the numeric maximum value, minimum value, and current-position value for the slider.
protected  BoundedRangeModel	sliderModel − The data model that handles the numeric maximum value, minimum value, and current-position value for the slider.
protected boolean snapToTicks − If true, the knob (and the data value it represents) resolve to the closest tick mark next to where the user positioned the knob.
protected boolean snapToTicks − If true, the knob (and the data value it represents) resolve to the closest tick mark next to where the user positioned the knob.
JSlider()
Creates a horizontal slider with the range 0 to 100 and an initial value of 50.
JSlider(BoundedRangeModel brm)
Creates a horizontal slider using the specified BoundedRangeModel.
JSlider(int orientation)
Creates a slider using the specified orientation with the range 0 to 100 and an initial value of 50.
JSlider(int min, int max)
Creates a horizontal slider using the specified min and max with an initial value equal to the average of the min plus max.
JSlider(int min, int max, int value)
Creates a horizontal slider using the specified min, max and value.
JSlider(int orientation, int min, int max, int value)
Creates a slider with the specified orientation and the specified minimum, maximum, and initial values.
void addChangeListener(ChangeListener l)
Adds a ChangeListener to the slider.
protected ChangeListener createChangeListener()
Subclasses that want to handle ChangeEvents from the model differently can override this to return an instance of a custom ChangeListener implementation.
Hashtable createStandardLabels(int increment)
Creates a Hashtable of numerical text labels, starting at the slider minimum, and using the increment specified.
Hashtable createStandardLabels(int increment, int start)
Creates a Hashtable of numerical text labels, starting at the starting point specified, and using the increment specified.
protected void fireStateChanged()
Send a ChangeEvent, whose source is this JSlider, to all ChangeListeners that have registered interest in ChangeEvents.
AccessibleContext	getAccessibleContext()
Gets the AccessibleContext associated with this JSlider.
ChangeListener[] getChangeListeners()
Returns an array of all the ChangeListeners added to this JSlider with addChangeListener().
int getExtent()
Returns the "extent" from the BoundedRangeModel.
boolean getInverted()
Returns true if the value-range shown for the slider is reversed.
Dictionary getLabelTable()
Returns the dictionary of what labels to draw at which values.
int getMajorTickSpacing()
This method returns the major tick spacing.
int getMaximum()
Returns the maximum value supported by the slider from the BoundedRangeModel.
int getMinimum()
Returns the minimum value supported by the slider from the BoundedRangeModel.
int getMinorTickSpacing()
This method returns the minor tick spacing.
BoundedRangeModel getModel()
Returns the BoundedRangeModel that handles the slider's three fundamental properties: minimum, maximum, value.
int getOrientation()
Return this slider's vertical or horizontal orientation.
boolean getPaintLabels()
Tells if labels are to be painted.
boolean getPaintTicks()
Tells if tick marks are to be painted.
boolean getPaintTrack()
Tells if the track (area the slider slides in) is to be painted.
boolean getSnapToTicks()
Returns true if the knob (and the data value it represents) resolve to the closest tick mark next to where the user positioned the knob.
SliderUI getUI()
Gets the UI object which implements the L&F for this component.
String getUIClassID()
Returns the name of the L&F class that renders this component.
int getValue()
Returns the slider's current value from the BoundedRangeModel.
boolean getValueIsAdjusting()
Returns the valueIsAdjusting property from the model.
protected String paramString()
Returns a string representation of this JSlider.
void removeChangeListener(ChangeListener l)
Removes a ChangeListener from the slider.
void setExtent(int extent)
Sets the size of the range "covered" by the knob.
void setFont(Font font)
Sets the font for this component.
void setInverted(boolean b)
Specify true to reverse the value-range shown for the slider and false to put the value range in the normal order.
void setLabelTable(Dictionary labels)
Used to specify what label will be drawn at any given value.
void setMajorTickSpacing(int n)
This method sets the major tick spacing.
void setMaximum(int maximum)
Sets the slider's maximum value to maximum.
void setMinimum(int minimum)
Sets the slider's minimum value to minimum.
void setMinorTickSpacing(int n)
This method sets the minor tick spacing.
void setModel(BoundedRangeModel newModel)
Sets the BoundedRangeModel that handles the slider's three fundamental properties: minimum, maximum, value.
void setOrientation(int orientation)
Set the slider's orientation to either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL.
void setPaintLabels(boolean b)
Determines whether labels are painted on the slider.
void setPaintTicks(boolean b)
Determines whether tick marks are painted on the slider.
void setPaintTrack(boolean b)
Determines whether the track is painted on the slider.
void setSnapToTicks(boolean b)
Specifying true makes the knob (and the data value it represents) resolve to the closest tick mark next to where the user positioned the knob.
void setUI(SliderUI ui)
Sets the UI object which implements the L&F for this component.
void setValue(int n) Sets the slider's current value to n.
void setValueIsAdjusting(boolean b)
Sets the model's valueIsAdjusting property.
protected void updateLabelUIs()
Updates the UIs for the labels in the label table by calling updateUI on each label.
void updateUI()
Resets the UI property to a value from the current look and feel.
This class inherits methods from the following classes −
javax.swing.JComponent
java.awt.Container
java.awt.Component
java.lang.Object
Create the following Java program using any editor of your choice in say D:/ > SWING > com > tutorialspoint > gui >
SwingControlDemo.java
package com.tutorialspoint.gui;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class SwingControlDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public SwingControlDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingControlDemo  swingControlDemo = new SwingControlDemo();      
      swingControlDemo.showSliderDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    
      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   private void showSliderDemo(){
      headerLabel.setText("Control in action: JSlider"); 
      JSlider slider = new JSlider(JSlider.HORIZONTAL,0,100,10);
      
      slider.addChangeListener(new ChangeListener() {
         public void stateChanged(ChangeEvent e) {
            statusLabel.setText("Value : " + ((JSlider)e.getSource()).getValue());
         }
      });
      controlPanel.add(slider);      
      mainFrame.setVisible(true);     
   } 
}
Compile the program using the command prompt. Go to D:/ > SWING and type the following command.
D:\SWING>javac com\tutorialspoint\gui\SwingControlDemo.java

If no error occurs, it means the compilation is successful. Run the program using the following command.
D:\SWING>java com.tutorialspoint.gui.SwingControlDemo

Verify the following output.

    30 Lectures 
     3.5 hours 

 Pranjal Srivastava

    13 Lectures 
     1 hours 

 Pranjal Srivastava

    25 Lectures 
     4.5 hours 

 Emenwa Global, Ejike IfeanyiChukwu

    14 Lectures 
     1.5 hours 

 Travis Rose

    14 Lectures 
     1 hours 

 Travis Rose
 Print
 Add Notes
 Bookmark this page