I l@ve RuBoard |
![]() ![]() |
14.2 Menu Bar Selection ModelsIn all GUI environments, menu components allow only one selection to be made at a time. Swing is no exception. Swing provides a data model that menu bars and menus can use to emulate this behavior: the SingleSelectionModel. 14.2.1 The SingleSelectionModel InterfaceObjects implementing the SingleSelectionModel interface do exactly what its name suggests: they maintain an array of possible selections and allow one element in the array to be chosen at a time. The model holds the index of the selected element. If a new element is chosen, the model resets the index representing the chosen element and fires a ChangeEvent to each of the registered listeners. 14.2.1.1 PropertiesObjects implementing the SingleSelectionModel interface contain the properties shown in Table 14-1. The selected property is a boolean that tells if there is a selection. The selectedIndex property is an integer index that represents the currently selected item.
14.2.1.2 EventsObjects implementing the SingleSelectionModel interface must fire a ChangeEvent (not a PropertyChangeEvent) when the object modifies its selectedIndex property, i.e., when the selection has changed. The interface contains the standard addChangeListener( ) and removeChangeListener( ) methods for maintaining a list of ChangeEvent listeners.
14.2.1.3 MethodThe SingleSelectionModel interface contains one other method:
14.2.2 The DefaultSingleSelectionModel ClassSwing provides a simple default implementation of the SingleSelectionModel interface in the DefaultSingleSelectionModel class. 14.2.2.1 PropertiesDefaultSingleSelectionModel contains just the properties required by the SingleSelectionModel interface, as shown in Table 14-2. The selectedIndex property is an integer index that represents the currently selected item. The default value of -1 indicates that there is no selection. The selected property is a boolean that returns true if the selectedIndex is anything other than -1, and false otherwise.
14.2.2.2 Events and methodsThe DefaultSingleSelectionModel object provides all the events and methods specified by the SingleSelectionModel interface discussed earlier. ![]() |
I l@ve RuBoard |
![]() ![]() |