![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
Some methods that you might find very useful are available in applications. The first is the setChecked() method. This allows you to either check or uncheck a menu entry. The following example shows how to turn on a check for a menu item: menuItem1.setChecked( true ); Its also going to be important at times to gray out your menu entries. You do this by setting its enabled flag to false. The method to use is the setEnabled() method, which takes a single Boolean argument. The following example shows how to gray out a menu item by using the setEnabled() method with an argument of false: menuItem1.setEnabled( false ); Adding Pop-Up Menus in AppletsIts easy to add pop-up menus in your applets. The Popup class offers pretty much all you need in order to do this. You first need to create the Popup object. The following example shows how to do just that: Popup m_Popup = new Popup(); Then, you have to add the pop-up entries. You use the Popup classs add() method to do this. The following three lines of source code show how to add three menu entries to a pop-up menu: m_Popup.add( "Canvas to Red" ); m_Popup.add( "Canvas to Green" ); m_Popup.add( "Canvas to Blue" ); Finally, you have to add your pop-up to a component. In the following example, Ive created a button and then added a Popup object to it: Button button = new Button( "Test Popup" ); button.add( m_Popup );
Listing 6.3 This Program Shows How to Attach a Pop-Up Menu to a Button 1 import java.awt.*; 2 import java.applet.*; 3 import java.awt.event.MouseListener; 4 import java.awt.event.*; 5 6 public class Applet1 extends Applet 7 { 8 PopupMenu m_Popup = new PopupMenu(); 9 10 public void init() 11 { 12 m_Popup.add( "Canvas to Red" ); 13 m_Popup.add( "Canvas to Green" ); 14 m_Popup.add( "Canvas to Blue" ); 15 16 MyButton button = new MyButton( "Test Popup" ); 17 button.add( m_Popup ); 18 add( button ); 19 } 20 21 public class MyButton extends Button 22 { 23 MyButton( String s ) 24 { 25 super( s ); 26 enableEvents( AWTEvent.ACTION_EVENT_MASK ); 27 } 28 29 public void processActionEvent( ActionEvent ae ) 30 { 31 m_Popup.show( this, 0, 0 ); 32 } 33 } 34 35 }
SummaryAdding pull-down menus to your programs is a fundamental part of providing users the kind of user interface theyre accustomed to. Its not that hard with Visual J++. The classes that let you attach menus to Frame windows are straightforward and easy to use. And responding to menu events in Visual J++ applications is almost trivial.
|
![]() |
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. |