Introduction.  Sliders are another of the many controls available in Java.  I am doing a webpage on them because I have made use of them in several of my more research oriented projects.

The following table contains a list of the constructors that are available for the JSlider class.


Constructor Description
JSlider(int, int) A slider with the specified minimum and maximum values
JSlider(int, int, int) A slider with the specified minimum and maximum values along with a starting value
JSlider(int, int, int, int) A slider with a specified orientation, minimum, maximum and starting value


Notice that you need at least two values in a JSlider constructor.

There are also a variety of methods that the developer will need to use in order to configure JSliders aprropriately.  Several of these methods are listed in the following table.


Method Description
setMajorTickSpacing(int) This sets the spacing between major tick marks on the slider.  This spacing is not done in pixels but in in values between the specified minimum and maximum values on the slider.
setMinorTickSpacing(int) This sets the spacing between minor tick marks.  Minor tick marks are half the length of major ones.
setPaintTicks(boolean) Determines whether the tick marks should be displayed.
setPaintLabels(boolean) Determines whether the numeric value on the tick marks should be displayed.


These specifications must be invoked before the JSlider is added to the GUI.

Now, we will make use of ChangeListeners when sensing user inputs relative to our sliders.  A brief discussion of these was given in the previous webpage.

Finding the Sum.  Now we present our first example, an applet called  This is a modification of a program we did last semester where we develop a GUI and rather than getting user inputs from JOptionPanes, we have the user adjust a JSlider.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class SliderSum extends JApplet

// computational inputs
long upperLimit;
// input labels, textfields
JLabel lblUpperLimit = new JLabel("Select how many positive integers to sum: ");
JTextField txtUpperLimit = new JTextField(7);

// labels and textfields outputs
JLabel lblSum = new JLabel("The Sum: ");
JTextField txtSum = new JTextField(8);

// slider to adjust the display for the upper limit
JSlider sldSliderInSystem = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);

public void init( )

// instantiate the grid that will coordinate the overall layout
JPanel panOverall = new JPanel();
GridLayout gblOverall = new GridLayout(5,1);

// develop label

// develop slider for upper limit of sum
SliderHandler sliderHandler = new SliderHandler();
// develop textbox to display the value selected on the slider
// add the label to display the sum
// add the textbox to display the sum



// method to find sum
public long FindSum(long sumUntil)

long computeSum = 0;
// looping to find the sum
for (int i=1; i <= sumUntil; i++)
computeSum = computeSum + i;

return computeSum;


private class SliderHandler implements ChangeListener

public void stateChanged (ChangeEvent e)

upperLimit = sldSliderInSystem.getValue( );




} // ends the SliderHandler class

} // ends the program


Since size impacts the display I am including the SliderSum.html file for the applet.


<applet code="SliderSum.class" width=500 height=200>


The following image represents the GUI with 12 selected on the JSlider.



Nothing in the code is particularly new, other than it creates its own class for handling ChangeEvents implementing the ChangeListener.

Notice that it is reasonably important to have the extra texfield to display the actual position of the slider.