![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
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 }
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 }
The TextField ControlThe 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 dont define an initial number of columns, the layout manager can use the TextField components initialTextValue() method to determine the TextField components appropriate length.
|
![]() |
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. |