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


The Choice Control

The Choice control generates the drop-down list. This is similar to what Windows programmers know as a combo box. Choice control allows one item to be selected at a time. An event is triggered when an item is selected. Choice control does not have sorting capabilities; therefore, items are positioned in the Choice list in the order in which they were added.

Constructors

There is only one constructor for a Choice control. It takes no arguments. The following example shows how to create a Choice control:

Choice choice = new Choice();

After a Choice control has been created, you use the addItem() method to add choices to the Choice controls list. The following example shows how to add a Choice control and add three items to this list:

Choice choice = new Choice();
choice.addItem( "Selection One" );
choice.addItem( "Selection Two" );
choice.addItem( "Selection Three" );

Choice Events

As with most controls that you can add to applets, you can handle events for Choice controls in two ways. The first way is to create your own class that extends the Choice control class. Inside this class, you add your own processItemEvent() method. This processItemEvent() method is what catches all the triggered events. In your extended class’s constructor, though, you must call the enableEvents() method and specify ITEM_EVENT_MASK. A simple example of a class that extends the Choice class follows. Notice that it has the constructor that calls the enableEvents() method, and that it adds its own processItemEvent() method.

public class MyChoice extends Choice
{
    MyChoice()
    {
        super();
        enableEvents( AWTEvent.ITEM_EVENT_MASK );
    }
    public void processItemEvent( ItemEvent ie )
    {
         // Perform tasks here…
    }
}

The second way of handling Choice events is to create your own class that implements an ItemListener interface. Then, you use the Checkbox’s addItemListener() method to add your newly created class that extends the ItemListener interface. The following example shows how to create a new class that implements the ActionListener class. Inside this class, an itemStateChanged() method was added. It’s inside of the itemStateChanged() method that you’ll perform your event handling.

public class MyListener implements ActionListener
{
    public void itemStateChanged( ItemEvent ie )
    {
        // Perform tasks here.
    }
}

Of course, you’ll have to use your Choice control’s addItemListener() method to add your newly created class. Without doing this, the class that implements the ActionListener interface will never get any events. The following example shows how to do this:

Choice choice = new Choice();
choice.addItemListener( new MyListener() );

Sample Programs That Handle Choice Control Events

I’ve created two programs that show how to use both methods to handle Choice control events. The first program, seen in Listing 7.6, extends the Choice class and lets the extended Choice class handle the events. The second one creates a class that implements the ActionListener interface.

Listing 7.6 This Sample Program Extends the Choice Class to Handle Choice Control 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   MyChoice m_Choice = new MyChoice();
10
11   public void init()
12   {
13       m_Choice.addItem( "Selection One" );
14       m_Choice.addItem( "Selection Two" );
15       m_Choice.addItem( "Selection Three" );
16       add( m_Choice );
17   }
18
19   public void paint( Graphics g )
20   {
21       g.drawString( m_strDisplay, 20, 100 );
22   }
23
24   public class MyChoice extends Choice
25   {
26       MyChoice()
27       {
28           super();
29           enableEvents( AWTEvent.ITEM_EVENT_MASK );
30       }
31
32       public void processItemEvent( ItemEvent ie )
33       {
34           m_strDisplay = "'" + getSelectedItem() + "' was selected.";
35           getParent().repaint();
36       }
37   }
38
39  }

When the program runs, you’ll see a single Choice control in the window, as well as a text string that displays the last event that was handled. Figure 7.4 shows the program running within Internet Explorer.


Figure 7.4  This program responds to Choice events by extending the Choice class.

The first thing I’d like to point out in this simple program is at line 24, where a new class that extends the Choice class is declared. Inside of the constructor, at line 29, the Choice class enables item events. At line 32, the Choice class adds a processItemEvent() method. Inside this method is where events are responded to. Now back up to line 9. This is where the Choice control is created. Lines 13, 14, and 15 show where the three different selections are added via the addItem() method. Finally, at line 16, the Choice control is added to the applet.

The second program in this section shows how to handle Choice events by creating a class that implements the ItemListener interface. The source code is given in Listing 7.7.


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.