• No results found

Constants

In document 107 Java Swing [ PUNISHER ] pdf (Page 91-96)

Chapter 4. Labels and Icons

4.2.1 The Icon Interface

5.1.2.3 Constants

DefaultButtonModel tracks its five boolean properties using a single state mask. Though they are

generally not useful outside the class, the constants used in this mask are defined as public integers, so we'll list them here. Note that none of the methods defined in DefaultButtonModel accept these

constants as method parameters. In short, you should never need these constants at all. Even if you subclass DefaultButtonModel for some reason, you should still use the public methods to access

these properties.

Table 5.4, DefaultButtonModel Constants

Constant Type Description

ARMED int The button is armed.

ENABLED int The button is enabled.

PRESSED int The button is pressed.

ROLLOVER int The button is rolled-over.

SELECTED int The button is selected.

5.1.2.4 Protected Fields

protected transient ChangeEvent changeEvent

protected EventListenerList listenerList

The listener list used to track all listeners added to the model.

protected int stateMask

The current state of properties armed, enabled, pressed, rollover, and selected. See

Section 5.1.3.3 for more information.

protected String actionCommand

protected ButtonGroup group

protected int mnemonic

The values of the properties of the same names.

5.1.2.5 Constructor

public DefaultButtonModel()

Creates a new model. The model's properties are set as defined in Table 5.3.

5.1.3 The AbstractButton Class

AbstractButton is an abstract base class for all button components (JButton, JToggleButton, JCheckBox, and JRadioButton, as well as JMenuItem and its subclasses).

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 just as they are with the JLabel class.

Image buttons may actually 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.1.3.1 Properties

The AbstractButton class defines the properties shown in Table 5.5.

Table 5.5, AbstractButton Properties

Property Data Type get is set bound Default Value

UI ButtonUI from L&F

model ButtonModel null

actionCommand String null

borderPainted boolean true

contentAreaFilled boolean true

disabledIcon Icon null

disabledSelectedIcon Icon null

enabled* boolean true

focusPainted boolean true

horizontalTextPosition int RIGHT

icon Icon null

label String

mnemonic int 0

margin Insets null

pressedIcon Icon null

rolloverEnabled boolean false

rolloverIcon Icon null

rolloverSelectedIcon Icon null

selected boolean false

selectedIcon Icon null

selectedObjects Object[] null

text String ""

verticalAlignment int CENTER

verticalTextPosition int CENTER

See also properties from the JComponent Class (xref linkend="SWING-CH-3-TABLE-10"/>).

There are seven different icons available for a button. Each is shown when the button is in a certain state, as defined below:[1]

[1] Actually, disabledSelectedIcon and rolloverSelectedIcon are ignored by the current Swing L&Fs. icon

The default icon for the button.

disabledIcon

The icon shown when the button is disabled (if not specified, a grayscale version of the default icon is generated automatically).

selectedIcon

The icon shown when the button has been selected.

disabledSelectedIcon

The icon shown when the button is selected and also disabled (if not specified, a grayscale version of the selected icon is generated; if no selected icon has been set, the disabled icon is used). If you're following closely, you'll notice that there's a potential problem here—if there is no disabledSelectedIcon defined and also no selectedIcon or disabledIcon

defined, the getDisabledSelectedIcon() method will return null. It would really be

more consistent to return a grayscale version of the default icon in this case.

pressedIcon

The icon shown while the button is being pressed.

The icon shown (if rolloverEnabled == true) when the cursor is moved over the

unselected button.

rolloverSelectedIcon

The icon shown (if rolloverEnabled == true) when the cursor is moved over the

selected button.

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 only significant when the button's size is larger than the default size. HorizontalTextPosition and verticalTextPosition specify the location of the text relative to the icon. These are only

meaningful if both an icon and text have been specified.[2]

[2] See the examples in Section 4.1, in Section 4.1.1, for further explanation of the alignment and text position properties.

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 affect, but if you

replace a button's border with one of your own, be aware that the margin space will not be used unless you access it explicitly in your border code. Model reflects the ButtonModel containing state

information about the button. The text property contains the text, if any, displayed on the button

(note that this property replaces the deprecated label property). BorderPainted indicates whether

or not a border (recall from Chapter 3, that border is a property of JComponent) should be painted

around the button. This assumes the button has a border defined. This is the default, but the border could be removed; then setting this property to true would still not cause a border to be drawn. The contentAreaFilled property indicates whether or not 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 the preferred mechanism, rather than calling setOpaque(false) because the value of the

opaque property for buttons is set by the L&F. FocusPainted indicates whether or not 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 or not moving the cursor over the button

should cause the rolloverIcon or rolloverSelectedIcon to be displayed. Calling setRolloverIcon() will cause this property to be set to true.

The actionCommand , mnemonic, and selected properties are taken directly from the AbstractButton's ButtonModel object. AbstractButton adds its own implementation of

setEnabled() , inherited from java.awt.Component which updates the enabled property of the ButtonModel.

UI holds the ButtonUI used to render the button.

5.1.3.2 Events

AbstractButton fires the events, required by the ButtonModel interface, and listed in 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 in the button's properties.

Event Description ActionEvent The button has been pressed.

ChangeEvent A change has occurred in one or more properties of the button's model.

ItemEvent The button has been toggled on or off. public void addActionListener(ActionListener l) public void removeActionListener(ActionListener l) public void addItemListener(ItemListener l)

public void removeItemListener(ItemListener l) >public void addChangeListener(ChangeListener l) public void removeChangeListener(ChangeListener l)

Several additional protected methods are defined to assist in the event firing process:

protected ActionListener createActionListener()

protected ChangeListener createChangeListener()

protected ItemListener createItemListener()

Used internally to create listeners to be added to the ButtonModel. Each listener uses the

button's "fire" methods (see below) to forward events fired by the model to the button's listeners. This allows users to add listeners to the button itself, rather than having to listen to the model. Subclasses wanting to change how model events are handled could override these methods.

protected void fireActionPerformed(ActionEvent e)

protected void fireItemStateChanged(ItemEvent e)

protected void fireStateChanged()

Use the standard EventListenerList to dispatch events to registered listeners. They are

called any time events are fired by the button model.

5.1.3.3 Constants

The following constants shown in Table 5.7 are defined by AbstractButton for use in PropertyChangeEvents.

Table 5.7, AbstractButton Constants

Constant Type Description

BORDER_PAINTED_CHANGED_PROPERTY Stringindicates that the has changed borderPainted property CONTENT_AREA_FILLED_CHANGED_PROPERTY Stringindicates that the property has changed contentAreaFilled DISABLED_ICON_CHANGED_PROPERTY Stringindicates that the has changed disabledIcon property DISABLED_SELECTED_ICON_CHANGED_PROPERTY Stringindicates that the property has changed disabledSelectedIcon FOCUS_PAINTED_CHANGED_PROPERTY Stringindicates that the has changed focusPainted property HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY Stringindicates that the property has changed horizontalAlignment HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY Stringindicates that the horizontalTextPosition property has

ICON_CHANGED_PROPERTY Stringindicates that the icon property has changed

MARGIN_CHANGED_PROPERTY Stringindicates that the changed margin property has MNEMONIC_CHANGED_PROPERTY Stringindicates that the mnemonic property has

changed

MODEL_CHANGED_PROPERTY Stringindicates that the model property has changed

PRESSED_ICON_CHANGED_PROPERTY Stringindicates that the changed pressedIcon property has ROLLOVER_ENABLED_CHANGED_PROPERTY Stringindicates that the property has changed rolloverEnabled ROLLOVER_ICON_CHANGED_PROPERTY Stringindicates that the has changed rolloverIcon property ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY Stringindicates that the rollovedSelectedIcon

property has changed

SELECTED_ICON_CHANGED_PROPERTY Stringindicates that the has changed selectedIcon property TEXT_CHANGED_PROPERTY Stringindicates that the text property has changed

VERTICAL_ALIGNMENT_CHANGED_PROPERTY Stringindicates that the property has changed verticalAlignment VERTICAL_TEXT_POSITION_CHANGED_PROPERTY Stringindicates that the property has changed verticalTextPosition

5.1.3.4 Protected Fields

protected ActionListener actionListener

protected ChangeListener changeListener

protected ItemListener itemListener

These fields hold the listeners responsible for receiving events from the model.

protected transient ChangeEvent changeEvent

The single instance of ChangeEvent used any time a ChangeEvent needs to be fired. There

is no need to create new ChangeEvent objects each time one is fired, since they contain no

information other than the object that fired them (this).

protected ButtonModel model

Provides direct access to the button's model.

5.1.3.5 Constructor public AbstractButton()

The default constructor does nothing.

In document 107 Java Swing [ PUNISHER ] pdf (Page 91-96)