16.3. The JApplet Class

 
[Page 498 ( continued )]

15.3. Check Boxes

A toggle button is a two-state button like a light switch. JToggleButton inherits AbstractButton and implements a toggle button. Often JToggleButton 's subclasses JCheckBox and JRadioButton are used to enable the user to toggle a choice on or off. This section introduces JCheckBox . JRadioButton will be introduced in the next section.

JCheckBox inherits all the properties from AbstractButton , such as text , icon , mnemonic , verticalAlignment , horizontalAlignment , horizontalTextPosition , verticalTextPosition , and selected , and provides several constructors to create check boxes, as shown in Figure 15.11.

Figure 15.11. JCheckBox defines a check box button.

Here is an example of a check box with text Student, red foreground, white background, mnemonic key 'S', and initially selected.

 JCheckBox jchk =   new   JCheckBox(   "Student"   ,   true   ); jchk.setForeground(Color.red); jchk.setBackground(Color.white); jchk.setMnemonic(   'S'   ); 

When a check box is clicked (checked or unchecked), it fires an ItemEvent and then an ActionEvent . To see if a check box is selected, use the isSelected() method.

Listing 15.3 gives a program that adds three check boxes named Centered , Bold , and Italic into the preceding example to let the user specify whether the message is centered, bold, or italic, as shown in Figure 15.12.

Figure 15.12. Three check boxes are added to specify how the message is displayed.
(This item is displayed on page 499 in the print version)

There are at least two approaches to writing this program. The first is to revise the preceding ButtonDemo class to insert the code for adding the check boxes and processing their events. The second is to create a subclass that extends ButtonDemo . Please implement the first approach as an exercise. Listing 15.3 gives the code to implement the second approach.


[Page 499]
Listing 15.3. CheckBoxDemo.java
(This item is displayed on pages 499 - 500 in the print version)


[Page 500]

CheckBoxDemo extends ButtonDemo and adds three check boxes to control how the message is displayed. When a CheckBoxDemo is constructed (line 12), its superclass's no-arg constructor is invoked, so you don't have to rewrite the code that is already in the constructor of ButtonDemo .

When a check box is checked or unchecked, the listener's actionPerformed method is invoked to process the event. When the "Centered" check box is checked or unchecked, the centered property of the MessagePanel class is set to true or false .

The current font name and size used in MessagePanel are obtained from messagePanel.getFont() using the getName() and getSize() methods . The font styles ( Font.BOLD and Font.ITALIC ) are specified in the check boxes. If no font style is selected, the font style is Font.PLAIN . Font styles are combined by adding together the selected integers representing the fonts.

The keyboard mnemonics 'C', 'B', and 'I' are set on the check boxes "Centered," "Bold," and "Italic," respectively (lines 22 “24). You can use a mouse gesture or a shortcut key to select a check box.

The setFont method (line 60) defined in the Component class is inherited in the MessagePanel class. This method automatically invokes the repaint method. Invoking setFont in messagePanel automatically repaints the message.

A check box fires an ActionEvent and an ItemEvent when it is clicked. You could process either the ActionEvent or the ItemEvent to redisplay the message. The example processes the ActionEvent . If you wish to process the ItemEvent , you could create a listener for ItemEvent and register it with a check box, as shown below:

   public class    CheckBoxDemoUsingItemEvent   extends   ButtonDemo  {  ... // Same as in CheckBoxDemo.java, so omitted    public   CheckBoxDemoUsingItemEvent() {  ... // Same as in CheckBoxDemo.java, so omitted   // To listen for ItemEvent   jchkCentered.addItemListener(   new   ItemListener() {   /** Handle ItemEvent */     public void   itemStateChanged(ItemEvent e)  {         messagePanel.setCentered(jchkCentered.isSelected());       }     });   } } 

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

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