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


Creating Menus in Applications

It’s a lot easier to create menus in applications than it is to create menus in applets. The reason is that all you have to do in an application is select the Menu control and place it on your form.

When you first put the Menu control on your form window, it will appear at the top of the form window, and you’ll see the text Type Here where the first menu item will go. This is shown in Figure 6.4.


Figure 6.4  This form shows an empty menu control that has been added.

It took me a while to get good at editing the menu item text and the menu entries. Where it says, Type Here, click the mouse and you can type the menu item text. If you press either Tab or the right-arrow key, the focus goes to the next menu item, and you can then type the text for that menu item. If you want to go ahead after you’ve typed the menu item text and type the menu entries, you’ll notice that below each menu item for which you’ve entered text, you’ll see a Type Here label. If you just select that and type your menu entry, the menu entry will be created with whatever text you type. This would be a really good time for you to create an application and put a Menu control on the application’s form to practice adding menu items, adding menu entries, and then going back to edit them. Adding a blank Menu control to your form doesn’t add very much code to it—in fact, it adds only two lines of code. The following line shows how I added a Menu control to my form and created the Menu control:

MainMenu mainMenu1 = new MainMenu();

Then, the Menu control added the menu to the form. The following example shows how the Menu control added the menu to the form:

this.setMenu(mainMenu1);

If, however, you add all the menu items and menu entries, a significant amount of code is added to your program. Including the two lines of code I just showed you, by adding three menu items having two, three, and two entries respectively, I added 36 lines of code to my form’s source code. When my program runs, it doesn’t look much different than the previous program I created that was an applet. Figure 6.5 shows my form with a functioning menu that has been added.


Figure 6.5  This program shows a form with a functioning menu added.

Adding Menu Events in Applications

Adding event-handler methods to applications for menu entries is very easy. To do this, select the menu entry you want to generate an event handler for. You do this in the Form window by just single-clicking on the menu entry. In the Properties window, be sure that it’s set to events. You do this by clicking the button that has the lightning bolt on it. You’ll see three events. The first event is the click event. This is triggered when the menu event is selected. The pop-up event is triggered before the containing menu is displayed. The select event, which is the one you’ll be interested in, is triggered when the menu event is selected. With the menu entry for which you want to generate the event selected, double-click on the select event. An event handler for that menu goes immediately to the source code for that handler.

This is how you add event handlers for application menu entries:

1.  Make sure that the Form window is in view.
2.  Select the menu entry you want to generate an event for by single-clicking on it.
3.  Make sure that the Properties window is set for events. Do this by clicking the event button.
4.  Double-click on the select event and the handler will be created. You’ll immediately see the source code for the handler after you’ve done this.

Presenting a Sample Application Program

I’ve taken all the previous examples for applications’ menus and created a sample program. This program performs exactly what the applet program performed. There are three menu items: File, Options, and Actions. Each one of them has some menu entries. The program then responds to the menu event and displays text in a label so that you know which menu entry was selected.

The program’s source code is shown in Listing 6.2.

Listing 6.2 A Simple Program That Uses a Menu and Responds to Menu Events

1   import com.ms.wfc.app.*;
2   import com.ms.wfc.core.*;
3   import com.ms.wfc.ui.*;
4   import com.ms.wfc.html.*;
5
6   /**
7    * This class can take a variable number of parameters on the command
8    * line. Program execution begins with the main() method. The class
9    * constructor is not invoked unless an object of type 'Form1' is
10   * created in the main() method.
11   */
12  public class Form1 extends Form
13  {
14      public Form1()
15      {
16          // Required for Visual J++ Form Designer support
17          initForm();
18
19          // TODO: Add any constructor code after initForm call
20      }
21
22      /**
23       * Form1 overrides dispose so it can clean up the
24       * component list.
25       */
26      public void dispose()
27      {
28          super.dispose();
29          components.dispose();
30      }
31
32      private void menuItem4_click(Object source, Event e)
33      {
34          label1.setText( "File/Open was selected" );
35      }
36
37      private void menuItem5_click(Object source, Event e)
38      {
39          label1.setText( "File/Close was selected" );
40      }
41
42      private void menuItem6_click(Object source, Event e)
43      {
44          label1.setText( "Options/Set to Red was selected" );
45      }
46
47      private void menuItem7_click(Object source, Event e)
48      {
49          label1.setText( "Options/Set to Green was selected" );
50      }
51
52     private void menuItem8_click(Object source, Event e)
53      {
54          label1.setText( "Options/Set to Blue was selected" );
55      }
56
57      private void menuItem9_click(Object source, Event e)
58      {
59          label1.setText( "Actions/Go Home was selected" );
60      }
61
62      private void menuItem10_click(Object source, Event e)
63      {
64          label1.setText( "Action/Go to Work was selected" );
65      }
66
67      /**
68       * NOTE: The following code is required by the Visual J++ form
69       * designer. It can be modified using the form editor. Do not
70       * modify it using the code editor.
71       */
72      Container components = new Container();
73      MainMenu mainMenu1 = new MainMenu();
74      MenuItem menuItem1 = new MenuItem();
75      MenuItem menuItem2 = new MenuItem();
76      MenuItem menuItem3 = new MenuItem();
77      MenuItem menuItem4 = new MenuItem();
78      MenuItem menuItem5 = new MenuItem();
79      MenuItem menuItem6 = new MenuItem();
80      MenuItem menuItem7 = new MenuItem();
81      MenuItem menuItem8 = new MenuItem();
82      MenuItem menuItem9 = new MenuItem();
83      MenuItem menuItem10 = new MenuItem();
84      Label label1 = new Label();
85
86      private void initForm()
87      {
88          menuItem4.setText("Open");
89          menuItem4.addOnClick(new EventHandler(this.menuItem4_click));
90
91          menuItem5.setText("Close");
92          menuItem5.addOnClick(new EventHandler(this.menuItem5_click));
93
94          menuItem1.setMenuItems(new MenuItem[] {
95                                 menuItem4,
96                                 menuItem5});
97          menuItem1.setText("File");
98
99          menuItem6.setText("Set to Red");
100         menuItem6.addOnClick(new EventHandler(this.menuItem6_click));
101
102         menuItem7.setText("Set to Green");
103         menuItem7.addOnClick(new EventHandler(this.menuItem7_click));
104
105         menuItem8.setText("Set to Blue");
106         menuItem8.addOnClick(new EventHandler(this.menuItem8_click));
107
108         menuItem2.setMenuItems(new MenuItem[] {
109                                menuItem6,
110                                menuItem7,
111                                menuItem8});
112         menuItem2.setText("Options");
113
114         menuItem9.setText("Go Home");
115         menuItem9.addOnClick(new EventHandler(this.menuItem9_click));
116
117         menuItem10.setText("Go to Work");
118         menuItem10.addOnClick(new EventHandler
            ⇒(this.menuItem10_click));
119
120         menuItem3.setMenuItems(new MenuItem[] {
121                                menuItem9,
122                                menuItem10});
123         menuItem3.setText("Actions");
124
125         mainMenu1.setMenuItems(new MenuItem[] {
126                                menuItem1,
127                                menuItem2,
128                                menuItem3});
129         /* @designTimeOnly mainMenu1.setLocation(new Point(0, 8)); */
130
131         this.setText("Form1");
132         this.setAutoScaleBaseSize(13);
133         this.setClientSize(new Point(292, 273));
134         this.setMenu(mainMenu1);
135
136         label1.setLocation(new Point(8, 48));
137         label1.setSize(new Point(272, 24));
138         label1.setTabIndex(1);
139         label1.setTabStop(false);
140         label1.setText("No action…");
141
142         this.setNewControls(new Control[] {
143                             label1});
144     }
145
146     /**
147      * The main entry point for the application.
148      *
149      * @param args Array of parameters passed to the application
150      * via the command line.
151      */
152     public static void main(String args[])
153     {
154         Application.run(new Form1());
155     }
156 }


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.