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


This first program is the one that extends the List control. The source code for it is given in Listing 7.8.

Listing 7.8 This Program Handles List Events by Extending the List Control

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 event.";
8
9       MyList m_List = new MyList( 10, false );
10
11      public void init()
12      {
13          m_List.addItem( "Selection One" );
14          m_List.addItem( "Selection Two" );
15          m_List.addItem( "Selection Three" );
16          add( m_List );
17      }
18
19      public void paint( Graphics g )
20      {
21          g.drawString( m_strDisplay, 20, 170 );
22       }
23
24      public class MyList extends List
25      {
26          MyList()
27          {
28              super();
29              enableEvents( AWTEvent.ITEM_EVENT_MASK );
30          }
31
32          MyList( int nLines, boolean bMultiselect )
33          {
34              super( nLines, bMultiselect );
35              enableEvents( AWTEvent.ITEM_EVENT_MASK );
36          }
37
38          public void processItemEvent( ItemEvent ie )
39          {
40              int nStateChange = ie.getStateChange();
41              if( nStateChange == ItemEvent.SELECTED )
42              {
43                  m_strDisplay = "'" + getSelectedItem()
                    ⇒+ "' was selected.";
44                  getParent().repaint();
45              }
46          }
47
48      }
49
50  }

When the program runs, you’ll see a list box that contains three items. Each time you select an item, the text string in the applet window shows the one you selected. Figure 7.5 shows the program running from within Internet Explorer.


Figure 7.5  This program responds to List events.

As with the other programs, I’d like to direct your attention to a point about halfway down the source code. Take a look at line 24. This is where the new class that extends the List class begins. Notice that at line 26 there’s a constructor that takes no arguments, and at line 32 there’s a constructor that takes two arguments. These match both constructors of the List class. Also notice that in each of these constructors, a call to the enableEvents() method was made. In line 38, you’ll find the processItemEvent() method. Notice that the first thing you do in this method is get an integer that represents the stateChange(). This could be selected or deselected, but for now we’ll focus only on the selected stage. Looking up now to line 9, we create the List object that was extended from the List class. Lines 13, 14, and 15 show where we add the selections to List control, and line 16 is where we add the List control to the applet.

This second sample program creates a class that implements the ItemListener interface. It then uses the addItemListener() method for the List control so that the List control knows where to send its events. The source code for this program is given in Listing 7.9.

Listing 7.9 This Program Uses a Class That Implements the ItemListener Interface to Handle List 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 event.";
8
9       List m_List = new List( 10, false );
10
11      public void init()
12      {
13          m_List.addItemListener( new MyListener() );
14          m_List.addItem( "Selection One" );
15          m_List.addItem( "Selection Two" );
16          m_List.addItem( "Selection Three" );
17          add( m_List );
18      }
19
20      public void paint( Graphics g )
21      {
22          g.drawString( m_strDisplay, 20, 170 );
23      }
24
25      public class MyListener implements ItemListener
26      {
27          public void itemStateChanged( ItemEvent ie )
28          {
29              if( ie.getStateChange() == ItemEvent.SELECTED )
30              {
31                  m_strDisplay = "'" + m_List.getSelectedItem()
                    ⇒+ "' was selected.";
32                  repaint();
33              }
34          }
35      }
36
37  }

This program will look exactly like the first List sample program. When you make a selection, the display string in the applet will tell you what selection you just made.

At line 25 of the source code, you’ll see that we created a new class that implements the ItemListener interface. In this class, at line 27, we added an itemStateChanged() method. It’s inside this method that we handled the events. Now back up to line 9. You’ll notice we created a normal List control, not an extended List control. Line 13 is where we add the Listener class to the List control with the addItemListener() method. Lines 14, 15, and 16 show where we add the List selections, and line 17 is where we add the List control to the applet.

The TextField Control

The TextField control enables the user to enter information into a text field. TextField controls can be created as empty or with an initial string. These controls can be defined to have an initial number of columns. If you don’t define an initial number of columns, the layout manager can use the TextField component’s initialTextValue() method to determine the TextField component’s appropriate length.


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.