You might be wondering just what a spinner is. It’s a new 1.4 component similar to the JComboBox, but it shows only one item. It includes up and down arrows to “scroll” through its set of values. A JFormattedTextField is used to edit and render those values. Spinners are quite flexible. They work nicely with a set of choices (such as the months of the year) as well as with unbounded ranges such as a set of integers.

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

class RolloverSpinnerListModel extends SpinnerListModel {

	public RolloverSpinnerListModel(Object[] items) {

	public RolloverSpinnerListModel(List items) {

	public Object getNextValue() {
		Object nv = super.getNextValue();
		if (nv != null) {
			return nv;
		return getList().get(0);

	public Object getPreviousValue() {
		Object pv = super.getPreviousValue();
		if (pv != null) {
			return pv;
		List l = getList();
		return l.get(l.size() - 1);

public class SpinnerTest extends JFrame {

	public SpinnerTest() {
		super("JSpinner Test");
		setSize(300, 180);

		Container c = getContentPane();
		c.setLayout(new GridLayout(0, 2));

		c.add(new JLabel(" Basic Spinner"));
		c.add(new JSpinner());

		c.add(new JLabel(" Date Spinner"));
		c.add(new JSpinner(new SpinnerDateModel()));

		String weekdays[] = new String[] { "Sunday", "Monday", "Tuesday",
				"Wednesday", "Thursday", "Friday", "Saturday" };
		c.add(new JLabel(" List Spinner"));
		c.add(new JSpinner(new SpinnerListModel(weekdays)));

		c.add(new JLabel(" Number Spinner"));
		c.add(new JSpinner(new SpinnerNumberModel(0, 0, 100, 5)));

		c.add(new JLabel(" Rollover List Spinner"));
		c.add(new JSpinner(new RolloverSpinnerListModel(weekdays)));


	public static void main(String args[]) {
		new SpinnerTest();

You may also like...