I l@ve RuBoard |
![]() ![]() |
5.3 The AbstractButton ClassAbstractButton is an abstract base class for all button components (JButton, JToggleButton, JCheckBox, JRadioButton, and JMenuItem and its subclasses). Since it provides functionality common to all types of buttons, we'll cover it here before getting to the concrete button classes. AbstractButton provides much of the functionality associated with the interaction between the various concrete button classes and their ButtonModel objects. As we mentioned earlier, buttons in Swing can be made up of an image (Icon ), text, or both. The relative positions of the text and icon are specified exactly as they are with the JLabel class. Image buttons may specify as many as seven different images, allowing the button to be displayed differently depending on its current state. The seven icons are described in Table 5-5, with the other properties defined by AbstractButton. 5.3.1 PropertiesThe AbstractButton class defines the properties shown in Table 5-5.
There are seven different icons available for a button. Each is shown when the button is in a certain state:[2]
The text property contains the text, if any, displayed on the button (note that this property replaces the deprecated label property). The model property is the ButtonModel containing the state information for the button. Setting the action property does a lot of work in one step. The newly attached Action receives any ActionEvents fired by the button, and the previous Action (if any) is deregistered. The button almost completely resets its properties based on the Action (see the configurePropertiesFromAction( ) method and Table 5-8). Furthermore, the button registers a PropertyChangeListener on the Action so it can automatically update itself when changes are made to the Action in the future. The horizontalAlignment and verticalAlignment properties specify where the button's content (text, icon, or both) should be drawn within the button's borders. These properties are significant only when the button is larger than the default size. horizontalTextPosition and verticalTextPosition specify the location of the text relative to the icon, and iconTextGap specifies the amount of space (in pixels) separating the text and the icon. These are meaningful only if both an icon and text have been specified.[3]
The multiClickThreshhold property is the length of time (in milliseconds) during which multiple mouse clicks are coalesced into a single ActionEvent. A value of 0 (the default) indicates that each click generates an ActionEvent no matter how quickly it follows its predecessor. The margin property specifies the distance between the button's borders and its contents (text, icon, or both). However, it's up to the border implementation to take advantage of the value of this property. The Swing L&Fs define borders that take the value of margin into account, but if you replace a button's border with one of your own, be aware that the margin space is not used unless you access it explicitly in your border code. borderPainted indicates whether a border (recall from Chapter 3 that border is inherited from JComponent) should be painted around the button. This property is meaningful only if the button actually has a border (it does by default). The contentAreaFilled property indicates whether the rectangular content area of the button should be filled. This should be set to false if you want to define an image-only button. Note that this is preferable to calling setOpaque(false) because the value of the opaque property for buttons is set by the L&F. focusPainted indicates whether something special (such as a dashed line inside the button's border) should be painted to show that the button has focus. Finally, the rolloverEnabled property indicates whether moving the cursor over the button should cause the rolloverIcon or rolloverSelectedIcon to be displayed. Calling setRolloverIcon( ) causes this property to be set to true. The actionCommand , mnemonic, and selected properties are taken directly from the AbstractButton's ButtonModel object. The displayedMnemonicIndex property behaves the same way it does in JLabel (see Section 4.1Section 4.1). AbstractButton adds its own implementation of setEnabled( ), inherited from java.awt.Component, which updates the enabled property of its ButtonModel. UI holds the ButtonUI used to render the button. 5.3.2 EventsAbstractButton fires the events required by the ButtonModel interface (see Table 5-6). An ActionEvent is fired when the button is pressed, an ItemEvent is fired when the button's state is changed, and a ChangeEvent is fired when a change has occurred to the button's properties.
All of these events are generated by the button's model. AbstractButton registers with the model as a listener for each type of event and refires any events fired by the model to any registered listeners. The following standard listener management methods are implemented in this class:
5.3.3 ConstantsThe constants shown in Table 5-7 are defined by AbstractButton for use in PropertyChangeEvents. Some PropertyChangeEvents generated by AbstractButton use strings other than these. There's no constant defined to indicate that the action property has changed, so the setAction( ) method fires a PropertyChangeEvent with the string "action". Accessibility-related change events use strings defined in the AccessibleContext class.
5.3.4 Public Methods
5.3.5 Action Configuration MethodsThese protected methods do most of the work to support Actions. Subclasses that wish to alter the way they behave with Actions should override these methods. (These methods were added in SDK 1.3.)
|
I l@ve RuBoard |
![]() ![]() |