![]() ![]()
|
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
Objectby clicking on up or down arrows. You can use aJCSpinBoxto spin through a list ofObjects (so long as the required editors and renderers exist, or have provided them), or use aJCSpinNumberBox, which can display any numeric object.
JCSpinBoxlooks very much like aJComboBoxexcept 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
JCSpinNumberBoxfor 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
JCSpinBoxModelinterface is used to manage the spin box's data.JCSpinBoxhas four constructors for the various ways in which you can supply data; that is, asObjects,Vectors, or via aJCSpinBoxModel. A fourth parameterless constructor is available. It uses an emptyDefaultSpinBoxModelas a placeholder for data that will be provided later.- Contents of a spin box may be modified via a
JCSpinBoxEditorinterface.- A
KeySelectionManagerinterface defines a method for associating a keystroke to an item in the spin box.JCSpinBoxModelmethods are inherited fromjavax.swing.ListModelandjavax.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
JCSpinBoxmodeled afterjavax.swing.ComboBoxModel.
JCSpinBoxModelis aListDataModelwith 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.ComboBoxModelandjavax.swing.ListModel.Extends
JCSpinBoxModelto 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, theJCSpinBoxModelinterface contains a data model forJCSpinBoxmodeled afterjavax.swing.ComboBoxModel.JCSpinBoxModelis aListDataModelwith 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
JCSpinBoxmethods.
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
setOperationmethod when you create anINTEGERversion of aJCSpinNumberBoxsince 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
JCNumberSpinBoxuses theJCValueModelinterface 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
valueChangedmethod:
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
![]() ![]()
|