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
Day 9 Dialogs
Today youll 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 arent magical or mysterious. Theyre 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 youll 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.
Todays lesson ends the user-interface section. After this, youll 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 youve created the program, youll 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, its 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 were 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 thats opened is Form. Make sure that the item thats selected is Form. In the Name field, type a meaningful name for your dialog. Id suggest About.java because thats 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.
Youll see the About.java file appear in the Project Explorer Window, and the About Form appear in the edit window. Youre 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 youre 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. Theres no response when you click the OK button. Youll learn more about handling those events later. Youll have to use the windows close button in the upper-right corner.
You might have noticed that the About dialog has minimize and maximize buttons. Dialogs by protocol dont. 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 dont 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.
Lets 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 youre with me 100%. For that reason, Ill 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 }
|