Spinners

Spinners allow users to select from a set of predefined options. Spinners contain ordered data. Spinners can be any of the following three types:

List Spinners

Spinner_List_Demo Example (Run Applet)

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

public class Spinner_List_Demo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel implements ChangeListener
    {
        private final String monthNames[] =
        {
            "January", "February", "March",
            "April", "May", "June",
            "July", "August", "September",
            "October", "November", "December"
        };
        private final SpinnerListModel monthModel = new SpinnerListModel(monthNames);
        private final JLabel message = new JLabel("");

        public View()
        {
            super();
            final JSpinner monthSpinner = new JSpinner(monthModel);
            this.setLayout(new BorderLayout());
            this.add("North", monthSpinner);
            this.add("South", this.message);

            monthSpinner.addChangeListener(this);
        }

        @Override
        public void stateChanged(ChangeEvent e)
        {
            this.message.setText(String.valueOf(this.monthModel.getValue()));
        }
    }
}

Number Spinners

Spinner_Number_Demo Example (Run Applet)

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

public class Spinner_Number_Demo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel implements ChangeListener
    {
        private final SpinnerNumberModel numberModel = new SpinnerNumberModel(5, // initial value
                                                                              1, //minimum in range
                                                                              10, //maximum in range
                                                                              1); //step size
        private final JLabel message = new JLabel("");

        public View()
        {
            super();

            final JSpinner numberSpinner = new JSpinner(this.numberModel);

            this.setLayout(new BorderLayout());
            this.add("North", numberSpinner);
            this.add("South", this.message);
            numberSpinner.addChangeListener(this);
        }

        @Override
        public void stateChanged(ChangeEvent e)
        {
            this.message.setText(String.valueOf(this.numberModel.getValue()));
        }
    }
}

Read Only Spinners

The spinner's value is set to editable by default. This means that users are able to delete any part of the spinner field. This does not cause a problem for ListSpinners. However, it does cause a problem for both NumberSpinners and DateSpinners. The system it does not replace the deleted text. This can be seen in the example above.

By setting the spinner to be read-only, you can ensure that a user does not delete the spinner's value. The code to do this is:

((JSpinner.DefaultEditor) spinnerName.getEditor()).getTextField().setEditable(false);

 

Spinner_NumberReadOnly_Demo Example (Run Applet)

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

public class Spinner_NumberReadOnly_Demo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel implements ChangeListener
    {
        private SpinnerNumberModel numberModel = new SpinnerNumberModel(5, // initial value
                                                                        1, //minimum in range
                                                                        10, //maximum in range
                                                                        1); //step size
        private JLabel message = new JLabel("");

        public View()
        {
            super();

            final JSpinner numberSpinner = new JSpinner(this.numberModel);

            // set the spinner to be read only
            ((JSpinner.DefaultEditor) numberSpinner.getEditor()).getTextField().setEditable(false);

            this.setLayout(new BorderLayout());
            this.add("North", numberSpinner);
            this.add("South", this.message);

            numberSpinner.addChangeListener(this);
        }

        @Override
        public void stateChanged(ChangeEvent e)
        {
            this.message.setText(String.valueOf(this.numberModel.getValue()));
        }
    }
}

Formatting Number Spinners

DateSpinners can be formatted in various ways, as shown in the example below.

Spinner_FormattedNumber_Demo Example (Run Applet)

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

public class Spinner_FormattedNumber_Demo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel implements ListSelectionListener
    {
        private final String formatNames[] =
        {
            "#",
            "0",
            "Year ####", // Any text
            "###,###.###",
            "###.##",
            "000000.000"
        };
        private final JList formatList = new JList(this.formatNames);
        private final int thisYear = Calendar.getInstance().get(Calendar.YEAR);
        private final SpinnerNumberModel numberModel = new SpinnerNumberModel(this.thisYear, // initial value
                                                                              thisYear - 5, //minimum in range
                                                                              thisYear + 5, //maximum in range
                                                                              1); //step size
        private final JSpinner numberSpinner = new JSpinner(this.numberModel);

        public View()
        {
            super();

            this.setLayout(new BorderLayout());
            this.add("North", this.formatList);
            this.add("South", this.numberSpinner);
            this.formatList.addListSelectionListener(this);
        }

        @Override
        public void valueChanged(ListSelectionEvent e)
        {
            this.numberSpinner.setEditor(new JSpinner.NumberEditor(this.numberSpinner, this.formatNames[this.formatList.getSelectedIndex()]));
        }
    }
}

Date Spinners

Spinner_Date_Demo Example (Run Applet)

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

public class Spinner_Date_Demo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel implements ChangeListener
    {
        private SpinnerDateModel dateModel;
        private JLabel message = new JLabel("");

        public View()
        {
            super();

            final Calendar calendar = Calendar.getInstance();

            final Date today = calendar.getTime();
            calendar.add(Calendar.YEAR, -5);
            final Date pastDate = calendar.getTime();
            calendar.add(Calendar.YEAR, 10);
            final Date futureDate = calendar.getTime();

            this.dateModel = new SpinnerDateModel(today, // initial value
                                                  pastDate, //minimum in range
                                                  futureDate, //maximum in range
                                                  Calendar.YEAR); //step size

            final JSpinner dateSpinner = new JSpinner(this.dateModel);

            this.setLayout(new BorderLayout());
            this.add("North", dateSpinner);
            this.add("South", this.message);

            dateSpinner.addChangeListener(this);
        }

        @Override
        public void stateChanged(ChangeEvent e)
        {
            this.message.setText(String.valueOf(this.dateModel.getValue()));
        }
    }
}

Formatting Date Spinners

DateSpinners can be formatted in various ways, as shown in the example below.

Spinner_FormattedDate_Demo Example (Run Applet)

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

public class Spinner_FormattedDate_Demo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel implements ListSelectionListener
    {
        private final String formatNames[] =
        {
            "dd.MM.yy",
            "dd MMMM yyyy",
            "yyyy.MM.dd G 'at' hh:mm:ss z",
            "E, MMM d, ''yy",
            "EEEE h:mm a",
            "H:mm a,z",
            "H:mm:ss:S",
            "yyyy.MMMM.dd G hh:mm a"
        };
        private final JList formatList = new JList(this.formatNames);
        private JSpinner dateSpinner;
        private final JLabel message = new JLabel("");

        public View()
        {
            super();

            this.formatList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

            final Calendar calendar = Calendar.getInstance();
            final Date today = calendar.getTime();
            calendar.add(Calendar.YEAR, -5);
            final Date pastDate = calendar.getTime();
            calendar.add(Calendar.YEAR, 10);
            final Date futureDate = calendar.getTime();

            final SpinnerDateModel dateModel = new SpinnerDateModel(today, // initial value
                                                                    pastDate, //minimum in range
                                                                    futureDate, //maximum in range
                                                                    Calendar.YEAR); //step size
            dateSpinner = new JSpinner(dateModel);

            setLayout(new BorderLayout());
            add("North", this.formatList);
            add("South", this.dateSpinner);

            this.formatList.addListSelectionListener(this);
        }

        @Override
        public void valueChanged(ListSelectionEvent e)
        {
            this.dateSpinner.setEditor(new JSpinner.DateEditor(this.dateSpinner, this.formatNames[this.formatList.getSelectedIndex()]));
        }
    }
}
 
<div align="center"><a href="../versionC/index.html" title="DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>