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


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 you’ll 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 16–18 show where we add the Selection items to the Choice control. Line 19 is where we add the Choice control to the applet.


Tip:  It’s not always necessary for Choice controls to handle events. You might be interested only in what selection has been made at certain times throughout your program’s execution. For instance, let’s say you want to do something, and at that time you need to know where the selection is set. All you need to do is call the Choice control’s getSelectedIndex() method, which returns an integer, or the Choice control’s getSelectedItem() method, which returns a string. In this way, you can save yourself a lot of code if it’s not necessary for your program to handle Choice control events.

The List Control

The 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.

Constructors

There 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 Events

You have two ways of handling List control events. They are very similar to the previous methods for the other controls we’ve mentioned. The first method involves extending the List control. In the extended class’s 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. It’s in the processItemEvent() method that you’ll get the event notifications. I’ve created a short example that extends the List class. In this example, I’ve 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, you’ll need to be sure to add an itemStateChanged() method. The short source-code example that follows shows how to declare this class. You’ll 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 Programs

In this section, I’ve 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.


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.