|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
Listing 7.7 This Sample Program Creates a Class That Implements the ActionListener Interface 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 Choice m_Choice = new Choice(); 10 11 MyListener Listener = new MyListener(); 12 13 public void init() 14 { 15 m_Choice.addItemListener( Listener ); 16 m_Choice.addItem( "Selection One" ); 17 m_Choice.addItem( "Selection Two" ); 18 m_Choice.addItem( "Selection Three" ); 19 add( m_Choice ); 20 } 21 22 public void paint( Graphics g ) 23 { 24 g.drawString( m_strDisplay, 20, 100 ); 25 } 26 27 public class MyListener implements ItemListener 28 { 29 public void itemStateChanged( ItemEvent ie ) 30 { 31 m_strDisplay = "'" + m_Choice.getSelectedItem() ⇒+ "' was selected."; 32 repaint(); 33 } 34 } 35 36 } This program will look exactly like the first sample program in this section. When you make a selection with the Choice control, the text string in the applet window will show you the event that was handled. The important thing to note in this program is at line 27. A new class is created that implements the ItemListener interface. Inside this class, at line 29, the itemStateChanged() method is added. Within the itemStateChanged() method, the event is handled. Now if youll look back at line 9, where the Choice control was created, we use the normal Choice control, not the extended Choice control of the earlier program. At line 11, we create the Listener class. At line 15, we tell the Choice object which listener to use by using the addItemListener() method. Lines 1618 show where we add the Selection items to the Choice control. Line 19 is where we add the Choice control to the applet.
The List ControlThe List control displays a number of selected items in a multiline list box. The number of rows to be displayed in a list can be specified in the constructor. The list can be defined to enable the user to select one item at a time or more than one item at a time. The List component triggers user events when items are selected or deselected. ConstructorsThere are two List constructors. The first one takes no arguments and simply creates a new scrolling list initialized with no visible lines and no multiple selections. The following example shows how to create a List that takes no arguments: List list = new List(); The second List constructor takes two arguments. The first argument specifies the number of visible lines that will be in the control. The second argument specifies whether multiple selections are allowed. The following example shows how to create a List control that has 10 visible lines and allows multiple selections: List list = new List( 10, true ); List Control EventsYou have two ways of handling List control events. They are very similar to the previous methods for the other controls weve mentioned. The first method involves extending the List control. In the extended classs constructor, you have to call the enableEvents() method to allow your extended class to catch item events. You also need to make sure that you have a processItemEvent() method. Its in the processItemEvent() method that youll get the event notifications. Ive created a short example that extends the List class. In this example, Ive chosen to implement both constructors so that I can use either one for my program. public class MyList extends List { MyList() { super(); enableEvents( AWTEvent.ITEM_EVENT_MASK ); } MyList( int nLines, boolean bMultiselect ) { super( nLines, bMultiselect ); enableEvents( AWTEvent.ITEM_EVENT_MASK ); } public void processItemEvent( ItemEvent ie ) { // Perform tasks here } } The second method is to create a class that implements the AddListener interface. Inside of this class, youll need to be sure to add an itemStateChanged() method. The short source-code example that follows shows how to declare this class. Youll also have to remember to use the addItemListener() method from your List class so that it knows which ItemListener to use. public class MyListener implements ItemListener { public void itemStateChanged( ItemEvent ie ) { // Perform tasks } } Sample ProgramsIn this section, Ive created two sample programs, showing different ways to handle List events. The first sample program extends the List class and lets the extended List class handle the event. The second sample program creates a class that implements the ItemListener interface and lets this handle the events.
|
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. |