• No results found

Fancy Buttons

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

Chapter 4. Labels and Icons

5.1.4 The JButton Class

5.1.4.7 Fancy Buttons

In the AbstractButton section, we learned that there are quite a few things that can be done with

Swing buttons to make them more visually interesting. In this example, we'll see how we can spice up a user interface by adding rollover and selected icons to our buttons. We'll also take away the button borders, focus painting, and filled content area to give our display a nice clean look.

//

FancyButton.java //

import javax.swing.*; import java.awt.*;

public class FancyButton extends JButton {

// Create a JButton that does not show focus, does not paint a border, and // displays different icons when rolled-over and pressed.

public FancyButton(Icon icon, Icon pressed, Icon rollover) { super(icon); setFocusPainted(false); setRolloverEnabled(true); setRolloverIcon(rollover); setPressedIcon(pressed); setBorderPainted(false); setContentAreaFilled(false); }

// A simple test program

FancyButton b1 = new FancyButton( new ImageIcon("images/redcube.gif"), new ImageIcon("images/redpaw.gif"), new ImageIcon("images/reddiamond.gif")); FancyButton b2 = new FancyButton(

new ImageIcon("images/bluecube.gif"), new ImageIcon("images/bluepaw.gif"), new ImageIcon("images/bluediamond.gif")); JFrame f = new JFrame();

f.addWindowListener(new BasicWindowMonitor()); Container c = f.getContentPane(); c.setLayout(new FlowLayout()); c.add(b1); c.add(b2); f.pack(); f.setVisible(true); } }

Figure 5.5 shows a few screenshots of our new button class with the different states of the buttons. Of course, this is just one example of a "fancy" button implementation. You can create your own special button classes using some or all of the features shown in FancyButton, as well as other

features, such as adding icons for other button states.

Figure 5.5. Buttons using "rollover" and "pressed" icons

5.1.5 The JToggleButton Class

JToggleButton is an extension of AbstractButton, used to represent buttons that can be toggled

on and off (as opposed to buttons like JButton which, when pushed, "pop back up"). It should be

noted that while the subclasses of JToggleButton (JCheckBox and JRadioButton) are the types of JToggleButtons most commonly used, JToggleButton is not an abstract class. When used

directly, it typically (though this is ultimately up to the L&F) has the appearance of a JButton that

does not pop back up when pressed (see Figure 5.6).

5.1.5.1 Properties

The JToggleButton class inherits all of its properties and most of its default values from its

superclasses. The exceptions are shown in Table 5.9. The model property is set to a new instance of ToggleButtonModel when a JToggleButton is created. ToggleButtonModel (described in the

next section) is a public inner class that extends DefaultButtonModel.

Table 5.9, JToggleButton Properties

Property Data Type get is set bound Default Value

model* ButtonModel ToggleButtonModel()

UIClassID* String "ToggleButtonUI"

accessibleContext*AccessibleContext JToggleButton.AccessibleJToggleButton()

See also properties from AbstractButton (xref linkend="SWING-CH-5-TABLE-12"/>).

5.1.5.2 Events

Like JButton, JToggleButton defines no new events. However, the events fired by

JToggleButtons are slightly different than those fired by JButton. Let's look at these events by

running a simple program like the one used in the JButton event section. This time, we'll create a JToggleButton instead of a JButton:

// JToggleButtonEvents.java // import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*;

public class JToggleButtonEvents {

public static void main(String[] args) {

JToggleButton jtb = new JToggleButton("Press Me"); jtb.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ev) { System.out.println("ActionEvent!");

} });

public void itemStateChanged(ItemEvent ev) { System.out.println("ItemEvent!");

} });

jtb.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent ev) { System.out.println("ChangeEvent!"); }

});

JFrame f = new JFrame();

f.addWindowListener(new BasicWindowMonitor()); Container c = f.getContentPane(); c.setLayout(new FlowLayout()); c.add(jtb); f.pack(); f.setVisible(true); } }

When we run this program and press the button, we get the following output:

ChangeEvent! ChangeEvent!

After releasing the button, we see:

ChangeEvent! ItemEvent! ChangeEvent! ActionEvent!

As in our JButton example, the first two events are fired to indicate that the button has been armed

and pressed. When the button is released, we get another change event, indicating that the button has now been selected. Additionally, toggle buttons fire an ItemEvent to indicate button selection.

The final two events match those of JButton, indicating that the button is no longer being pressed

and that an action (button press) has occurred.

Subsequent button presses result in one less ChangeEvent (just like we saw with JButton), because

the button remains armed after it has been pressed.

5.1.5.3 Constructors public JToggleButton()

Creates a button that has no text or icon and is not selected.

public JToggleButton(Icon icon)

public JToggleButton(Icon icon, boolean selected)

Create a button that displays the specified icon. If included, the boolean parameter indicates

the initial selected state of the button.

public JToggleButton(String text)

public JToggleButton(String text, boolean selected)

Create a button that displays the specified text. If included, the boolean parameter indicates

public JToggleButton(String text, Icon icon)

public JToggleButton(String test, Icon icon, boolean selected)

Create a button that displays the specified text and icon. If included, the boolean parameter

indicates the initial selected state of the button.

5.1.5.4 User Interface Method

public void updateUI()

Indicates that the look-and-feel (L&F) has changed.

5.1.6 The JToggleButton.ToggleButtonModel Class

As we mentioned earlier, JToggleButton does not use the DefaultButtonModel class as its

model. ToggleButtonModel, a public static inner class that extends DefaultButtonModel, is used

instead.

5.1.6.1 Properties

ToggleButtonModel modifies the methods for working with the properties listed in Table 5.10.

New implementations of isSelected() and setSelected() are provided that use the button's ButtonGroup (if defined) to keep track of which button is selected. This is done to ensure that even

if multiple selected buttons are added to a group, only the first one will be considered selected (since the group keeps track of the "officially" selected button). In addition, the setPressed()

method is redefined to call setSelected() when the button is released (if it is armed).

Table 5.10, JToggleButton.ToggleButtonModel Properties

Property Data Type get is set bound Default Value

pressed* boolean false

selected* boolean false

See also properties from DefaultButtonModel (xref linkend="SWING-CH-5-TABLE-6"/>).

5.1.7 The JCheckBox Class

The look-and-feels for the JCheckBox[3] class are shown in Figure 5.7. JCheckBox is a subclass of JToggleButton typically used to allow the user to turn a given feature on or off, or to make

multiple selections from a set of choices. A JCheckBox is usually rendered by showing a small box

into which a "check" is placed when selected (as shown in Figure 5.7). If you specify an icon for the checkbox, this icon replaces the default box. Therefore, if you choose to specify an icon, you should always also supply a selected icon—otherwise, there will be no way to tell if a check box has been selected or not .

[3] Note the difference in capitalization from AWT's Checkbox class.

5.1.7.1 Properties

The JCheckBox class inherits all of its properties and most of its default values from its

superclasses. The only exceptions are shown in Table 5.11. By default, no border is painted on

JCheckBoxes, and their horizontalAlignment is to the left.

Table 5.11, JCheckBox Properties

Property Data Type get is set bound Default Value

UIClassID* String "CheckBoxUI"

accessibleContext* AccessibleContext AccessibleJCheckBox

borderPainted* boolean false

horizontalAlignment* int LEFT

See also properties from JToggleButton (xref linkend="SWING-CH-5-TABLE-20"/>).

5.1.7.2 Events

See the discussion of JToggleButton (JCheckBox's superclass) events.

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