GuidesCreating a Spin Control in Java

Creating a Spin Control in Java content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

We are all aware of the spin control that just shows one value in a text field and has two buttons having an up arrow and a down arrow, this component usually controls a property in an application that has an increasing or a decreasing value, or a property that has distinctly different values. For example, a spin control could allow you to select font sizes, or sampling interval for seconds, minutes, hours, or select the brightness of a color on a pallete.

But couldn’t a JComboBox or a JList do the job?

Yes, but the key here is a “two-step selection” process in both JComboBox and JList:

  1. You open up the choices in a combo (or scroll down a list) and
  2. Then select your choice.

In certain situations, this may not be desirable as both of these methods require some space on the UI.

Moreover, if your options are continuous and your current selection is “1”, and all you want is to increase the value to “2”, you would prefer to just click on the increment button and have value “2” take effect, rather than pop a combo and select “2”.

JList in the javax.swing package almost fills the bill, because by default the event listeners it registers expect a “selection” on the list, like

(remember the two-step process).

Our idea is to reduce this two-step process, i.e., scrolling and selecting, to a one-step process.

Fortunately, we do not have to look very far, our JList and JScrollPane provide us with interesting possibilities.

Let us say we have these choices to show to the user:

final String data[] = {“One”,”Two”,”Three”,”Four”,”Five”};

(final because we are going to access this in an inner class)

Then we make our spin object:

final JList spinList = new JList(data);

As JList doesn’t scroll by default, we have to put it in a ScrollPane’s viewport.

JScrollPane scrollPane = new JScrollPane(spinList);

Now the JScrollPane as we know has JScrollBars, which are the protected members of JScrollPane. It also has the accessor methods

and getHorizontalScrollbar() to get a handle to the scrollbars. And these will go into the making of our spin control.

We simply add an adjustmentListener to the JScrollBar that we obtained from the JScrollPane.

scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener(){
public void adjustmentValueChanged(AdjustmentEvent ae){
} );

In one swoop, we add an anonymous inner class that adds a listener to the horizontal scrollbar, which by scrolling captures the visible choice (which is constrained to be “one” by


centerPanel is an object of class CenterPanel, which is there to demonstrate the capability of Spin control by changing its graphics text.

I hope this example gives you an idea as to how to make your own spin control whenever you feel that a Combo or a List is not suitable.

We could have made our own custom Component SpinControl class, which would be extended from Component and have generated a custom event, say, SpinEvent, as well as a SpinListener interface, and the SpinEvent could have contained the constants, like SpinEvent.UP and SpinEvent.DOWN. But why go to such trouble? Java provides a lot under the hood.

   Create your spin control
 @author Nasir Khan

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

public class SpinTest extends JFrame {
 private PaintPanel centerPanel;
 private JPanel topPanel;
	Container container = getContentPane();
	container.add(centerPanel = new PaintPanel()); //addToCenter

	// Can use any of the Constructors of JList
	// here we are using an Array of Strings
	final String data[] = {"One","Two","Three","Four","Five"};
      final JList spinList = new JList(data);

	// Constrain the visible rows to one

      // Put the list in the scrollpane to enable scrolling
	   JScrollPane scrollPane = new JScrollPane(spinList);

	// get the JScrollBar, add an AdjustmentListener to it
	public void adjustmentValueChanged(AdjustmentEvent ae){
	} );

	// This holds our spin control
	topPanel = new JPanel();
	topPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

		} //constructor
public static void main(String args[]){
	SpinTest spinTest = new SpinTest();
	spinTest.setSize(new Dimension(300,300));
	} //main
} //class SpinTest

 *	A Class made to display the capability of spin control
class PaintPanel extends JPanel {
	private String message="";
public void setPaintText(String message){
	this.message = message;
	} //setPaintText
public void paintComponent(Graphics g){
	g.setFont(new Font("SansSerif",Font.BOLD,20));
	} //paintComponent
} //class PaintPanel

About the Author

Nasir Khan is a Sun Certified Java programmer, with a B.E. in electrical engineering and a masters degree in systems. His areas of interest include Java applications, EJB, RMI, CORBA, JDBC and JFC. He is presently working with BayPackets Inc. in high technology areas of telecom software.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories