5.5 The JToggleButton Class


JToggleButton is an extension of AbstractButton and is 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 kinds 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).

Figure 5-6. JToggleButtons in four L&Fs
figs/swng2.0506.gif

5.5.1 Properties

The JToggleButton class inherits all of its properties and most of its default values from its superclass. The exceptions are shown in Table 5-10. 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-10. JToggleButton properties

Property

Data type

get

is

set

Default value

accessibleContexto

AccessibleContext

·

   

JToggleButton.AccessibleJToggleButton( )

modelo

ButtonModel

·

 

·

ToggleButtonModel( )

UIClassIDo

String

·

   

"ToggleButtonUI"

ooverridden

See also properties from AbstractButton (Table 5-5).

5.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!");       }     });     jtb.addItemListener(new ItemListener( ) {       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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     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 is 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 is pressed. (Depending on the L&F, there may also be additional ChangeEvents.)

5.5.3 Constructors

public JToggleButton( )

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

public JToggleButton(Action a)

Create a button with property values taken from the specified Action (see Table 5-8), register the Action to receive ActionEvents fired by the button, and register the button as a ChangeListener of the Action. The button adapts to any future changes made to the Action. This is equivalent to instantiating a JToggleButton with the default constructor and then calling its setAction( ) method. (This constructor was introduced with SDK 1.3.)

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

Create a button that displays the specified icon. If included, the boolean parameter determines the initial selection 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 determines the initial selection state of the button.

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 determines the initial selection state of the button.



Java Swing
Graphic Java 2: Mastering the Jfc, By Geary, 3Rd Edition, Volume 2: Swing
ISBN: 0130796670
EAN: 2147483647
Year: 2001
Pages: 289
Authors: David Geary

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net