![]() ![]() ![]() |
12
Spin Boxes
Features of JCSpinBox and JCSpinNumberBox
Classes and Interfaces
Properties
Constructors and Methods
Examples
12.1 Features of JCSpinBox and JCSpinNumberBox
Swing provides checkboxes and radio buttons, but no spin boxes. The JClass spin boxes fill the need for components that let the user select a number or an
Object
by clicking on up or down arrows. You can use aJCSpinBox
to spin through a list ofObject
s (so long as the required editors and renderers exist, or have provided them), or use aJCSpinNumberBox
, which can display any numeric object.
JCSpinBox
looks very much like aJComboBox
except that it has no dropdown. It takes a list of objects and presents these values in a spin box. You use the up and down arrows to cycle through the list.- Use
JCSpinNumberBox
for incrementing and decrementing objects of typejava.lang.Number
. You can select numbers of typeByte
,Short
,Integer
,Long
, orFloat
, and you can set maximum and minimum values for the spin operation.- Both components follow Swing's MVC paradigm. A
JCSpinBoxModel
interface is used to manage the spin box's data.JCSpinBox
has four constructors for the various ways in which you can supply data; that is, asObject
s,Vector
s, or via aJCSpinBoxModel
. A fourth parameterless constructor is available. It uses an emptyDefaultSpinBoxModel
as a placeholder for data that will be provided later.- Contents of a spin box may be modified via a
JCSpinBoxEditor
interface.- A
KeySelectionManager
interface defines a method for associating a keystroke to an item in the spin box.JCSpinBoxModel
methods are inherited fromjavax.swing.ListModel
andjavax.swing.ComboBoxModel
. These areaddListDataListener
,getElementAt
,getSize
,removeListDataListener
,getSelectedItem
, andsetSelectedItem
.- The listener is the
addValueListener
, and the event isJCValueEvent
.
12.2 Classes and Interfaces
Interfaces
A data model for
JCSpinBox
modeled afterjavax.swing.ComboBoxModel
.
JCSpinBoxModel
is aListDataModel
with a selected item. This selected item is in the model since it is not always in the item list. It inherits its methods fromjavax.swing.ComboBoxModel
andjavax.swing.ListModel
.Extends
JCSpinBoxModel
to define models that are changeable. It declares methods for adding, inserting, and removing elements.Helper Classes
12.3 Properties
JCSpinBox properties
These properties contain all of the functionality of
JCSpinBox
. In keeping with Swing's MVC design paradigm, theJCSpinBoxModel
interface contains a data model forJCSpinBox
modeled afterjavax.swing.ComboBoxModel
.JCSpinBoxModel
is aListDataModel
with a selected item. This selected item is in the model since it is not always in the item list.JCSpinNumberBox properties
These properties let you specify the operation, that is, whether the numbers in the spin box are whole numbers or floating point numbers. Additionally, you can set the spin increment and bounds.
For a complete list of properties, please see Properties of JCSpinBox and Properties of JCSpinNumberBox in Appendix A.
12.4 Constructors and Methods
Constructors
JCSpinBox methods
These methods manage a list of items by providing methods for adding and removing items from the list of objects, and for adding listeners for these changes. See the API for the complete list of
JCSpinBox
methods.
12.5 Examples
To use a
JCSpinNumberBox float_spin = new JCSpinNumberBox();JCSpinNumberBox
, simply instantiate it and set its parameters according to your needs, for example:
float_spin.setName("FloatingPointSpinBox");
float_spin.setValue(new Integer(0));
float_spin.setValueRange(new JCSpinNumberBox.Range(new Integer(0), new Integer(12)));
float_spin.setSpinStep(new Double(1.5));
float_spin.setOperation(float_spin.FLOATING_POINT);You don't need to use the
setOperation
method when you create anINTEGER
version of aJCSpinNumberBox
since that is the default type.Similarly, you can create a
JCSpinBox string_spin = new JCSpinBox(titles);JCSpinBox
:
string_spin.setName("StringSpinBox");
string_spin.setSelectedIndex(0);
string_spin.addValueListener(listener);The figure shows that each time a mouse click changes a spin box's value, the generated event can report on both the old and the new value. The output in Figure 42 results from clicking each spin box in succession twice.
Figure 42 : Capturing spin box events.
Listening for Spin Box Events
JCNumberSpinBox
uses theJCValueModel
interface to set and get its values, and to define its listeners. To respond to spin events, do something like this:
- Create a listener as part of the setup for the component, for example:
- Then add a listener to the spin box:
- Implement the listener class and define a
valueChanged
method:
class ValueListener implements JCValueListener {
"
public void valueChanging(JCValueEvent e) {
}
public void valueChanged(JCValueEvent e) {
System.out.println(((Component) e.getSource()).getName() + " changed from " +e. getOldValue() +
to " +
e.getNewValue());
}
} // end of ValueListener
![]() ![]() ![]() |