![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
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.
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 }
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. |