![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
The Choice ControlThe 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. ConstructorsThere 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 EventsAs 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 classs 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 Checkboxs 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. Its inside of the itemStateChanged() method that youll perform your event handling. public class MyListener implements ActionListener { public void itemStateChanged( ItemEvent ie ) { // Perform tasks here. } } Of course, youll have to use your Choice controls 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 EventsIve 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.
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 }
|
![]() |
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. |