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


When the program runs, you’ll see a menu with the three menu items. Each time you select a menu entry, the label in the Form window will show what you just selected, as shown in Figure 6.6.


Figure 6.6  This application has a menu with three items in response to the menu entry events.

At line 32, the event handler for the first menu item’s first menu entry can be seen. The menu event handlers go all the way through line 65; there’s one event handler for each menu entry. Line 73 shows where the Main menu is created. Lines 74–83 show where the menu items are created. As you can see in lines 88–128, a lot of source code was added to create and populate the menu.

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 );

It’s 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 Applets

It’s 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 class’s 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, I’ve created a button and then added a Popup object to it:

Button button = new Button( "Test Popup" );
button.add( m_Popup );

I’ve created a simple program, shown in Listing 6.3, that attaches a pop-up menu to a button. To handle the button event and show the pop-up menu when the button was pressed, I had to extend my Button class. This topic is covered in detail on Day 7, “Applet User Interface Controls,” so for now I’ll just mention it without explaining it in-depth.

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  }

When the program runs, you’ll see a single button in the applet window. When you click the button, a pop-up menu with three choices will appear.

Summary

Adding pull-down menus to your programs is a fundamental part of providing users the kind of user interface they’re accustomed to. It’s 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.


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.