The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Creating a GUI with JFC/Swing
Lesson: Using Swing Components

How to Use Spinners

In release 1.4, a new Swing component called the spinner was introduced. Spinners are similar to combo boxes(in the Creating a GUI with JFC/Swing trail) and lists(in the Creating a GUI with JFC/Swing trail) in that they let the user choose one from a range of values. Like editable combo boxes, spinners can allow the user to type in a value. Unlike combo boxes and lists, however, spinners force users to select values in a particular order. Except for the case where the user types in a value, the user must step through the possible values (using onscreen arrow buttons or the keyboard's arrow keys) one at a time, in a predefined sequence.

Here are some examples of spinners from various look and feels:

Windows spinner CDE/Motif spinner Java look and feel spinner Mac Acqua spinner
Windows CDE/Motif JLF Mac Aqua

[PENDING: One day, this page will tell you how to use spinners, with examples and tables of the spinner API. Until then, you can find information about spinners from JSpinner - A Simple Sequence Container(outside of the tutorial), which is part of the J2SE v 1.4 release notes.]

The following tables list some of the commonly used API for using spinners.

Classes Related to Spinners
Class or Interface Purpose
JSpinner A single-line input field that allows the user to select a number or object value from an ordered sequence.
SpinnerModel The interface implemented by all spinner models.
AbstractSpinnerModel The usual superclass for spinner model implementations.
SpinnerListModel A subclass of AbstractSpinnerModel whose values are defined by an array or a List.
SpinnerDateModel A subclass of AbstractSpinnerModel that supports sequences of Dates.
SpinnerNumberModel A subclass of AbstractSpinnerModel that supports sequences of numbers.

Useful JSpinner Methods
Method or Constructor Purpose
JSpinner()
JSpinner(SpinnerModel)
Create a new JSpinner. The no-argument constructor creates a spinner with an integer SpinnerNumberModel with an initial value of 0 and no minimum or maximum limits. The optional parameter on the second constructor allows you to specify your own SpinnerModel.
void setValue(java.lang.Object)
Object getValue()
Set or get the currently displayed element of the sequence.
Object getNextValue()
Object getPreviousValue()
Get the object in the sequence that comes after or before the object returned by getValue.

SpinnerListModel Methods
Method Purpose
void setList(List)
List getList()
Set or get the List that defines the sequence for this model.

SpinnerDateModel Methods
Method Purpose
void setValue(Object)
Date getDate()
Object getValue()
Set or get the current Date for this sequence.
void setStart(Comparable)
Comparable getStart()
Set or get the first Date in this sequence.
void setEnd(Comparable)
Comparable getEnd()
Set or get the last Date in this sequence.
void setCalendarField(int)
int getCalendarField()
Set or get the size of the date value increment used by the getNextValue and getPreviousValue methods. The specified parameter must be one of the following constants, defined in Calendar: ERA, YEAR, MONTH, WEEK_OF_YEAR, WEEK_OF_MONTH, DAY_OF_MONTH, DAY_OF_YEAR, DAY_OF_WEEK, DAY_OF_WEEK_IN_MONTH, AM_PM, HOUR_OF_DAY, MINUTE, SECOND, MILLISECOND.

SpinnerNumberModel Methods
Method Purpose
void setValue(Object)
Number getNumber()
Set or get the current value for this sequence.
void setMaximum(Comparable)
Comparable getMaximum()
Set or get the upper bound for numbers in this sequence. If the maximum is null, there is no upper bound.
void setMinimum(Comparable)
Comparable getMinimum()
Set or get the lower bound for numbers in this sequence. If the minimum is null, there is no lower bound.
void setStepSize(Number)
Number getStepSize()
Set or get the increment used by getNextValue and getPreviousValue methods.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.