Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Sample Programs

I’ve created two sample programs to show how to add check boxes to an applet, and how to handle the events. The first program extends the Checkbox class and lets the extended class handle the events. The second program adds an ItemListener and lets a new class (which implements the ItemListener interface) handle the events.

The source code for the first check box demo program follows in Listing 7.3.

Listing 7.3 The First CheckboxDemo Program Shows the Extended Checkbox Class

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       Checkbox m_Checkbox1 = new Checkbox( "One" );
10      Checkbox m_Checkbox2 = new Checkbox( "Two" );
11      Checkbox m_Checkbox3 = new Checkbox( "Three" );
12      MyCheckbox m_Checkbox4 = new MyCheckbox( "Four" );
13      MyCheckbox m_Checkbox5 = new MyCheckbox( "Five" );
14      MyCheckbox m_Checkbox6 = new MyCheckbox( "Six" );
15
16      public void init()
17      {
18          add( m_Checkbox1 );
19          add( m_Checkbox2 );
20          add( m_Checkbox3 );
21          add( m_Checkbox4 );
22          add( m_Checkbox5 );
23          add( m_Checkbox6 );
24      }
25
26      public void paint( Graphics g )
27      {
28          g.drawString( m_strDisplay, 20, 100 );
29      }
30
31      public class MyCheckbox extends Checkbox
32      {
33          String m_strText;
34
35          MyCheckbox( String s )
36          {
37              super( s );
38              m_strText = s;
39              enableEvents( AWTEvent.ITEM_EVENT_MASK );
40          }
41
42          public void processItemEvent( ItemEvent ie )
43          {
44              m_strDisplay = "'" + m_strText + "' was just pressed. ";
45              getParent().repaint();
46              super.processItemEvent( ie );
47          }
48
49      }
50
51  }

When the program runs, you’ll see six check boxes and some text in the window that indicates the last action or event. Check boxes one, two, and three don’t produce any events. Check boxes four, five, and six do produce events. You can see the program running within Internet Explorer in Figure 7.2.


Figure 7.2  This program generates and handles events for check boxes four, five, and six, and shows what the last event was in the applet window.

The most important thing you should notice about the program source code is line 31, where the declaration for the MyCheckbox class is found. The MyCheckbox class extends Checkbox, and the reason we’ve extended Checkbox is so that we can handle the Checkbox events inside the extended Checkbox class. Notice that at line 35, we’ve implemented our own constructor that takes a string. If we wanted to use any of the other constructors, such as the ones that take two and three arguments, we would have to implement additional constructors. However, for this simple program, the only constructor we’ll need is the one that takes a String argument. Notice at line 39 that we’re enabling the ITEM_EVENT_MASK. Also, at line 42, we’ve added the processItemEvent() method to which all events will go. Looking back up at lines 9, 10, and 11, we’re creating three simple check boxes. Lines 12, 13, and 14 show we’re creating three more check boxes through our extended Checkbox class. Already you can see that the first three check boxes will not be able to handle events, whereas the fourth, fifth, and sixth check boxes will. Lines 18–23 simply add all six check boxes to that container.

And now comes the second sample program, shown in Listing 7.4, in which we create a class that implements the ItemListener interface.

Listing 7.4 The Second CheckboxDemo Program Uses an ItemListener to Handle Checkbox Events

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       Checkbox m_Checkbox1 = new Checkbox( "One" );
10      Checkbox m_Checkbox2 = new Checkbox( "Two" );
11      Checkbox m_Checkbox3 = new Checkbox( "Three" );
12      Checkbox m_Checkbox4 = new Checkbox( "Four" );
13      Checkbox m_Checkbox5 = new Checkbox( "Five" );
14      Checkbox m_Checkbox6 = new Checkbox( "Six" );
15
16      MyListener Listener = new MyListener();
17
18      public void init()
19      {
20          m_Checkbox4.addItemListener( Listener );
21          m_Checkbox5.addItemListener( Listener );
22          m_Checkbox6.addItemListener( Listener );
23          add( m_Checkbox1 );
24          add( m_Checkbox2 );
25          add( m_Checkbox3 );
26          add( m_Checkbox4 );
27          add( m_Checkbox5 );
28          add( m_Checkbox6 );
29      }
30
31      public void paint( Graphics g )
32      {
33          g.drawString( m_strDisplay, 20, 100 );
34      }
35
36      public class MyListener implements ItemListener
37      {
38          public void itemStateChanged( ItemEvent ie )
39          {
40              if( ie.getSource() == m_Checkbox4 )
41              {
42                  m_strDisplay = "'Four' was pressed.";
43                  repaint();
44              }
45              else if( ie.getSource() == m_Checkbox5 )
46              {
47                  m_strDisplay = "'Five' was pressed.";
48                  repaint();
49              }
50              else if( ie.getSource() == m_Checkbox6 )
51              {
52                  m_strDisplay = "'Six' was pressed.";
53                  repaint();
54              }
55          }
56      }
57
58  }


Previous Table of Contents Next


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.