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


Day 9
Dialogs

Today you’ll learn how to create, display, manipulate, and interact with dialog boxes. Next to menus, dialog boxes (referred to for the rest of the chapter as dialogs) are the most important user-interface item. Dialogs communicate with users. They notify users of things they need to know, and ask for information that the program needs.

Dialogs aren’t magical or mysterious. They’re just windows that are treated in a slightly different way. Mainly, dialogs are modal windows that prevent users from doing anything else in an application until the dialog has been closed.

Today you’ll complete these tasks:

  Learn how to create and display a dialog.
  Learn about methods that are important when programming dialogs.
  Find out what other important methods will help you when dealing with Forms in general.

Today’s lesson ends the user-interface section. After this, you’ll have user-interface programming well under control.

Creating a Dialog

Creating a dialog is the same as creating a Form. Editing a dialog is the same as editing a Form. Displaying and interacting is the main difference. To start, create a new project named AboutDialog. Do that now.

When you’ve created the program, you’ll see the default form, named Form1. Complete source code is given later, in Listings 9.1 and 9.2, but for you to learn this topic thoroughly, it’s far more valuable for you to perform these steps as you read through them. Double-click on Form1 and have it in the edit window. Now, double-click on the form so that you get a Form1_click() method.


Tip:  The reason we want the Form1_click() method is so that we have a convenient place to create and display the dialog. This is the easiest way to get a program to give you a place to test something. Keep this in mind when you want to test things in your Visual J++ applications.

Now we’re ready to add a dialog to the project. We really add a form, not a dialog specifically. Select Project, Add Form from the menu. Make sure that the category that’s opened is Form. Make sure that the item that’s selected is Form. In the Name field, type a meaningful name for your dialog. I’d suggest About.java because that’s what I did. Figure 9.1 shows the Add Item dialog right before I added the About.java Form to my project.


Figure 9.1  Notice that the Form category is open and the Form item is selected. The name I added is About.java.

You’ll see the About.java file appear in the Project Explorer Window, and the About Form appear in the edit window. You’re ready to start adding controls to the Form. I added two, a label and a button, as shown in Figure 9.2.


Figure 9.2  This Form has two controls: a label and a button.

The rest is easy. In the Form1_click() method, we create the Form with the new operator and then call its showDialog() method. The code I added follows. If you’re following along with the discussion, go ahead and add it to your program. Compile the application and then run it.

private void Form1_click(Object source, Event e)
{
    About dlg = new About();
    dlg.showDialog();
}

When the program runs, you see a blank Form. When you click in the blank Form, the About dialog comes up. There’s no response when you click the OK button. You’ll learn more about handling those events later. You’ll have to use the window’s close button in the upper-right corner.

You might have noticed that the About dialog has minimize and maximize buttons. Dialogs by protocol don’t. You need to stick with the standard Windows dialog presentation and remove the minimize and maximize buttons. To do this, go to the About Form properties. Set the minimizeBox and maximizeBox properties to false.


Caution:  Dialogs don’t ordinarily have minimize and maximize buttons. Your program will look maladjusted if your dialogs have these two buttons. Be sure to remove them from all of your dialogs.

Let’s review the basics of creating and displaying a dialog for a Visual J++ application. The summary steps follow:

1.  Start with a Visual J++ application.
2.  Add a Form and name it something meaningful. The best way to name dialogs is based on what they do.
3.  To create the dialog, use the new operator.
4.  To show a modal dialog (which 99.5% of them are), make a call to the showDialog() method.

I want to make absolutely sure that you’re with me 100%. For that reason, I’ll show you the program I have so far. The source code is given in Listings 9.1 and 9.2.

Listing 9.1 The Source Code for Form1 in our Dialog Program

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      }
20
21      /**
22       * Form1 overrides dispose so it can clean up the
23       * component list.
24       */
25      public void dispose()
26      {
27          super.dispose();
28          components.dispose();
29      }
30
31      private void Form1_click(Object source, Event e)
32      {
33          About dlg = new About();
34          dlg.showDialog();
35      }
36
37      /**
38       * NOTE: The following code is required by the Visual J++ form
39       * designer.  It can be modified using the form editor.  Do not
40       * modify it using the code editor.
41       */
42      Container components = new Container();
43
44      private void initForm()
45      {
46          this.setText("Form1");
47          this.setAutoScaleBaseSize(13);
48          this.setClientSize(new Point(292, 273));
49          this.addOnClick(new EventHandler(this.Form1_click));
50      }
51
52      /**
53       * The main entry point for the application.
54       *
55       * @param args Array of parameters passed to the application
56       * via the command line.
57       */
58      public static void main(String args[])
59      {
60          Application.run(new Form1());
61      }
62  }


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.