I l@ve RuBoard |
![]() ![]() |
5.5 The JToggleButton ClassJToggleButton 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![]() 5.5.1 PropertiesThe 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.
5.5.2 EventsLike 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
![]() |
I l@ve RuBoard |
![]() ![]() |