I l@ve RuBoard |
![]() ![]() |
7.9 Spinner ModelsThe javax.swing package includes several pre-built models for many common data types suited to spinners. Figure 7-15 shows the hierarchy of these models. Figure 7-15. SpinnerModel class diagram![]() 7.9.1 The SpinnerModel InterfaceThe SpinnerModel interface includes methods required to successfully store and retrieve spinner data. It includes a read/write value and next and previous properties, and it forces implementing models (such as AbstractSpinnerModel) to support a ChangeListener. 7.9.1.1 PropertiesNot surprisingly, the properties for SpinnerModel are centered on the value being shown in the spinner. Notice in Table 7-17 that the model stores only the current value and the next/previous values. The actual list (or other object) behind these values is not part of the model.
7.9.1.2 EventsAny changes to the selected value should be reported through ChangeEvent objects.
7.9.2 The AbstractSpinnerModel ClassThe AbstractSpinnerModel class implements the event parts of the SpinnerModel. Implementations of addChangeListener( ), removeChangeListener( ), and getChange-Listeners( ) are all present. Two expected methods, fireStateChange( ) and get-Listeners( ), have been added. All the other models subclass AbstractSpinnerModel, as shown in Figure 7-15. 7.9.3 The SpinnerDateModelIf you're retrieving dates from users, a date spinner can make the input process much simpler. You can supply minimum and maximum dates along with an increment value (to increment by day, week, month, year, etc.). 7.9.3.1 PropertiesTable 7-18 shows the properties for SpinnerDateModel. Apart from the properties inherited from AbstractSpinnerModel, start and end properties have been added to make it possible to work within a bounded range of dates. Either of these properties can be set to null to indicate that you do not want a minimum or maximum. The calendarField property determines the increment/decrement step size and uses constants defined in the java.util.Calendar class. The valid step sizes are shown in Table 7-19. The date property is a convenience property that allows you to retrieve the current value of the spinner as a Date object.
7.9.3.2 Constructors
7.9.4 The SpinnerListModel ClassThis model allows you to spin through the (String representation of) items in a List (or an array—which gets turned into a List). When you hit the start or the end, trying to go past them results in a null next or previous value that effectively stops the spinner from spinning; in other words, you can't go past the bounds of the array. The Javadoc makes a point worth repeating here: the model stores only a reference to the list of items, not a copy. If the list changes, it's up to the programmer to deal with the consequences. The benefit, of course, is that the items shown by the spinner stay in near-perfect sync with the list. We say near-perfect because if you're sitting on the item that changed, it won't show up until you spin away and spin back. 7.9.4.1 PropertiesThe only new property added to SpinnerListModel is, not surprisingly, the list itself. See Table 7-20.
7.9.4.2 ConstructorsThree constructors exist for creating new SpinnerListModel objects:
7.9.5 The SpinnerNumberModel ClassThe number model allows you to spin numbers (both integers and decimals). The range can be bounded, or you can selectively leave off the minimum, maximum, or both. While you normally use Number objects to fill the model, special case constructors exist for the very common int and double types. For those types, you specify the starting position, the minimum and maximum, and the step size. (Note that for doubles, the step size is also a double, so you can increment by 0.1, 0.05, 2.5, etc.) 7.9.5.1 PropertiesTable 7-21 shows the properties for the number model. Beyond the standard properties, minimum and maximum properties are added to provide a range for the spinner. As with the SpinnerDateModel, either of these values can be null to indicate that no limit exists. The stepSize property allows you to specify the increment/decrement value for the spinner. The number property is a convenience property that allows you to retrieve the current value as a Number object.
7.9.5.2 Constructors
7.9.6 A Custom Model: Rollover ListsAs an example of how simple it can be to extend the functionality of these spinner models, here's a RolloverListModel that you can use. Like the SpinnerListModel, it takes a list, but rather than return null if you try to go past the end (or the beginning, for that matter), it "rolls over" to the beginning (or the end). Here's the source code for this model: // RolloverSpinnerListModel.java // import javax.swing.*; import java.util.List; public class RolloverSpinnerListModel extends SpinnerListModel { public RolloverSpinnerListModel(Object[] items) { super(items); } public RolloverSpinnerListModel(List items) { super(items); } public Object getNextValue( ) { Object nv = super.getNextValue( ); if (nv != null) { return nv; } return getList( ).get(0); } public Object getPreviousValue( ) { Object pv = super.getPreviousValue( ); if (pv != null) { return pv; } List l = getList( ); return l.get(l.size( ) - 1); } } This model is used for the last spinner shown in Figure 7-13. In that example, we use the weekdays array for both a standard list model and this rollover list model. You'll have to play with the spinner to get the effect—static screen shots just don't do it justice. |
I l@ve RuBoard |
![]() ![]() |