Header javaperspective.com
JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.22. Spinners

5.22. Spinners
Last updated: 1 January 2013.

This tutorial will show you how to use spinners in Java.

A spinner is an instance of the class JSpinner. It is a component that allows the user to select a number, date or string value from an ordered sequence. The selected value is displayed in an editor. By default, the editor is a panel containing a JFormattedTextField although it can be any subclass of JComponent. Next to the editor, there are two arrows: an up and a down arrow. The up arrow displays the next value in the sequence whereas the down arrow displays the previous value in the sequence. Alternatively, the user can also press the up and down arrow keys of the keyboard to step up and down within the sequence. The following picture shows a frame containing several spinners:


Spinners



The possible values of a spinner are defined by a model. Hence, to build a spinner, you must provide a model, that is, a subclass of the class AbstractSpinnerModel. The following standard models are provided by the JDK:The next sample is the code that builds the spinners shown in the above picture:

import java.awt.GridLayout;
import java.text.DateFormatSymbols;
import java.util.Arrays;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public final class Spinners extends JFrame {

   
public Spinners() {
         
init();
          addComponents
();

          setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
          setVisible
(true);
   
}


   
private void addComponents() {
         
// Set up the content pane
         
JPanel contentPane = new JPanel(new GridLayout(0, 2, 10, 10));
          contentPane.setBorder
(BorderFactory.createEmptyBorder(10, 10, 10, 10));

         
// Set up the day label
         
JLabel dayLabel = new JLabel("Day spinner", SwingConstants.TRAILING);
          contentPane.add
(dayLabel);

         
// Set up the day spinner
         
String[] days = new DateFormatSymbols().getWeekdays();
          SpinnerModel dayModel =
new SpinnerListModel(Arrays.asList(days).subList(1, 8));
          JSpinner daySpinner =
new JSpinner(dayModel);
          contentPane.add
(daySpinner);

         
// Set up the age label
         
JLabel ageLabel = new JLabel("Age spinner", SwingConstants.TRAILING);
          contentPane.add
(ageLabel);

         
// Set up the age spinner
         
SpinnerModel ageModel = new SpinnerNumberModel(30, 15, 130, 1);
          JSpinner ageSpinner =
new JSpinner(ageModel);
          contentPane.add
(ageSpinner);

         
// Set up the date label
         
JLabel dateLabel = new JLabel("Date spinner", SwingConstants.TRAILING);
          contentPane.add
(dateLabel);

         
// Set up the date spinner
         
SpinnerModel dateModel = new SpinnerDateModel();
          JSpinner dateSpinner =
new JSpinner(dateModel);
          dateSpinner.setEditor
(new JSpinner.DateEditor(dateSpinner, "dd/MM/yyyy"));
          contentPane.add
(dateSpinner);

         
// Add the content pane to the JFrame
         
add(contentPane);
   
}


   
private void init() {
         
setTitle("Spinners");
          setSize
(250, 150);
          setLocationRelativeTo
(null);
   
}


   
public static void main(String[] args){
         
SwingUtilities.invokeLater(new Runnable() {
               
public void run() {
                     
new Spinners();
               
}
          })
;
   
}
}

Let's take a look at the date spinner: I first created an instance of SpinnerDateModel with the default constructor that takes no arguments. As a result, the date spinner's editor is initialized with the current date, no limits are specified for the start/end values and the field of the date that is incremented or decremented is by default Calendar.DAY_OF_MONTH. You can use the constructor SpinnerDateModel(Date value, Comparable start, Comparable end, int calendarField) to specify other settings as you like. After creating the SpinnerDateModel, I passed it to the JSpinner constructor. Next, I called the method setEditor(JComponent editor) to specify a date format. Otherwise, if no date format is specified, the default date format includes the hour and minutes.

The selected value of a spinner is returned by the method getValue. It is possible to listen to value changes of a spinner by registering a change listener on the spinner or the model. As a basic example, the following sample registers a change listener on the date spinner. Each time the value within the spinner changes, the new value is printed to the standard output.

dateSpinner.addChangeListener(new ChangeListener() {
   
public void stateChanged(ChangeEvent e) {
         
System.out.println(dateSpinner.getValue());
   
}
})
;


You are here :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.22. Spinners
Next tutorial :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.23. Separators

Copyright © 2013. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7