|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
When this program runs, it will look identical to the earlier program. Check boxes one, two, and three will generate no events, whereas check boxes four, five, and six will generate events. The text string in the applet window will show the last event that was handled. Once again, well start about halfway down the program with the analysis because its very important. At line 36, we declare our own class, named MyListener, which implements the ItemListener interface. At line 38, we add the itemStateChanged() method. This is where all the events will go when they are triggered. Inside the itemStateChanged() method, we use the Item events getSource() method to find out which instantiated object triggered the event. We make a comparison, and depending on which one triggered the event, we respond by displaying the event to the window. Lines 914 show where we create the six check boxes that we add to the applet window. Theyre all normal Checkbox objects; none of them is an extended Checkbox object. In line 16, we instantiate one of the MyListener classes. In lines 2022, we add the ItemListener by using the addItemListener() method to check boxes four, five, and six. Just as in the earlier program, you can see that theres no way that check boxes one, two, and three will handle events because we dont add the ItemListener to them. Finally, lines 2328 simply add the check boxes to the applet. Checkbox GroupsIf you combine check boxes into a Checkbox group, youll get check boxes with a different look. Whereas check boxes are normally square, the Checkbox groups (usually referred to as radio buttons) are normally round. Besides the difference in appearance, they function differently. After you add check boxes into a Checkbox group, only one of the check boxes can be selected at a time. ConstructorsThere is only one Checkbox group constructor. It takes no arguments. The following line of code shows how to create a Checkbox group: CheckboxGroup group = new CheckboxGroup(); Before we go on, though, lets revisit the Checkbox constructors. Theres a Checkbox constructor thats especially meant for creating check boxes that will be grouped together. It takes three arguments: a string, a Checkbox group, and an initial state. If you specify an initial state of true for more than one check box in a group, the last one for which you specify true will be shown as selected. Grouping Check BoxesCheckbox groups handle no events on their own. However, the check boxes that are grouped into them handle events in the exact same manner as was described in the preceding section. Theres absolutely no difference in event handling for check boxes, whether theyre in a group or not. Here are the steps to creating check boxes and associating them all in a group:
The following short example shows how to group three check boxes: CheckboxGroup group = new CheckboxGroup(); Checkbox Checkbox1 = new Checkbox( "One", group, true ); Checkbox Checkbox2 = new Checkbox( "Two", group, false ); Checkbox Checkbox3 = new Checkbox( "Three", group, false ); Sample ProgramBecause handling events for check boxes that are grouped in a Checkbox group is exactly the same for check boxes that are not in a group, the purpose of this example is not to show you how to handle events, but to show you how to group the check boxes together. Ive taken the first sample program from the Checkbox section and altered it so that the first three buttons are grouped together and the last three buttons are grouped together. This altered program is given in Listing 7.5. No substantive change to the event-handling code was made. Therefore, check boxes one, two, and three will not trigger any events, but check boxes four, five, and six will. Listing 7.5 A Modified Program Showing How to Group Check Boxes 1 import java.awt.*; 2 import java.applet.*; 3 import java.awt.event.*; 4 5 public class Applet1 extends Applet 6 { 7 String m_strDisplay = "No events"; 8 9 CheckboxGroup m_Group1 = new CheckboxGroup(); 10 CheckboxGroup m_Group2 = new CheckboxGroup(); 11 Checkbox m_Checkbox1 = new Checkbox( "One", m_Group1, true ); 12 Checkbox m_Checkbox2 = new Checkbox( "Two", m_Group1, false ); 13 Checkbox m_Checkbox3 = new Checkbox( "Three", m_Group1, false ); 14 MyCheckbox m_Checkbox4 = new MyCheckbox( "Four", m_Group2, true ); 15 MyCheckbox m_Checkbox5 = new MyCheckbox( "Five", m_Group2, false ); 16 MyCheckbox m_Checkbox6 = new MyCheckbox( "Six", m_Group2, false ); 17 18 public void init() 19 { 20 add( m_Checkbox1 ); 21 add( m_Checkbox2 ); 22 add( m_Checkbox3 ); 23 add( m_Checkbox4 ); 24 add( m_Checkbox5 ); 25 add( m_Checkbox6 ); 26 } 27 28 public void paint( Graphics g ) 29 { 30 g.drawString( m_strDisplay, 20, 100 ); 31 } 32 33 public class MyCheckbox extends Checkbox 34 { 35 String m_strText; 36 37 MyCheckbox( String s, CheckboxGroup group, boolean state ) 38 { 39 super( s, group, state ); 40 m_strText = s; 41 enableEvents( AWTEvent.ITEM_EVENT_MASK ); 42 } 43 44 public void processItemEvent( ItemEvent ie ) 45 { 46 m_strDisplay = "'" + m_strText + "' was just pressed. "; 47 getParent().repaint(); 48 super.processItemEvent( ie ); 49 } 50 51 } 52 53 } When the program runs, as shown in Figure 7.3, youll notice right away that the check boxes have a different appearance. When you start clicking on the check boxes, youll also notice that their behavior is very different. Because the first three check boxes are grouped together, only one of those first three can be selected at once. The same holds true for the fourth, fifth, and sixth check boxes. Youll also see the text string in the applet window change only when you select fourth, fifth, and sixth radio-button check boxes.
There arent many differences between the source code for this program and that for the first check box sample program, but notice that lines 9 and 10 create two Checkbox groups. Then notice that lines 11, 12, and 13 use the Checkbox constructor that takes three arguments, one of which is the group to which the check box will belong. Lines 14, 15, and 16 call the MyCheckbox constructor that also takes three arguments, one of which is the group to which this Checkbox belongs. Remember that previously the constructor took only a single argumentthe label String. Youll notice in line 37 that we had to modify the constructor. So now, not only does it take a String, but it also takes a Checkbox group in an initial state. Lines 2025, as always, have the Checkbox controls to the applet; and line 33, as before, is where the declaration of the extended Checkbox class begins.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |