In the previous two chapters you learned about Java's user interface classes and how to use them. You learned how to combine layout managers and panels to position components at the right place on the screen. You also learned how to write code that would create menus. In this chapter you will take a detour. Instead of learning how to write Java code to solve a specific problem, you will learn how to avoid writing code by using another of the wizards built into Visual J++. In this chapter you are introduced to the Resource Wizard. The Resource Wizard enables you to visually design your screens and menus, thereby avoiding some of the work you went through in the preceding chapters.
In college I had a math professor who would teach by filling a blackboard with complicated formulas and rules for solving a type of problem. He'd then work through a sample problem that took the entire class session and involved a seemingly infinite number of steps. If you forgot a step, you could forget getting the right answer. Invariably, after a couple of days of this the professor would start the next class session by announcing, "We've been doing it the hard way so far; now I'll show you the easy way." In this chapter you will learn the easy way of designing dialogs and menus.
The Resource Wizard enables you to visually lay out and design your dialogs, frames, panels, and menus. Rather than having to worry about Java's layout managers and how to combine them to place components on the screen, you use a simple drag-and-drop editor. This enables you to see what you are creating as you create it. For example, Figure 7.1 shows a screen that has had OK and Cancel buttons already placed on it and a List is being positioned over the dialog.
Figure 7.1 : Dragging a List onto a dialog in the Dialog Editor.
To use the Resource Wizard you first create a resource template file, which will hold the container and menu resources used by your applet. You use the Visual J++ Dialog Editor to edit container resources (for example, panels, frames, and dialogs) and the Visual J++ Menu Editor to create and edit menus.
A resource template file (which ends with the .RCT extension) is then processed by the Resource Wizard. The Resource Wizard generates Java code that you can add to your project workspace, which enables you to use the dialogs and menus you designed.
Because a typical applet usually has more user interface components (such as text fields, buttons, and lists) than menus, the most common use of the Resource Wizard will be to assist in creating these components. If you are coming to Visual J++ from a Windows programming background, the way you think about dialog-like resources will have to change. In Windows programming, a program's resources are stored in a resource file that is compiled and linked into the program. In Java, this is not the case. Java dialogs are created at runtime using code such as the following:
Button okButton = new Button("OK");
TextField firstName = new TextField(15);
The Resource Wizard acts as a bridge between these two approaches.
The Resource Wizard can read a resource template file and will
generate the Java code necessary to display the components as
designed in the Dialog Editor.
| NOTE |
The resource template files (*.RCT) created by Visual J++ are stored in the same format as the files written by Visual C++. If you are converting a program from Visual C++ to Visual J++, you can take advantage of this and not have to re-create existing dialogs. |
The Resource Wizard creates a control creator class for
each Dialog resource. A control creator class is used
to place controls on any Java Container class. Controls
are sometimes called components, but are always subclasses of
the Java Component class that was introduced in Chapter
5, "Java's User Interface Components." A control creator
class is a very flexible idea and can be used to place controls
on any Java Container-not just Dialog. You can use a
control creator class to place controls on a Panel, Frame,
Applet, or Dialog.
| TIP |
The naming of some of the components in Visual J++ is unfortunate. This is a legacy that results from Visual J++ sharing so much of the Developer Studio with Visual C++. Although it is called the Dialog Editor and it works on Dialog resources, the classes generated by the Resource Wizard based on these resources do not need to be used in Java dialogs. Remember, the control creator classes made by the Resource Wizard can be used to place Java components on any Container class, including Applet, Dialog, Panel, and Frame. |
Each control creator class has two public methods: a constructor and CreateControls. The constructor is passed a Container onto which the controls will be placed. The CreateControls method takes care of actually placing the controls on the Container.
When you place controls in the Dialog Editor, you do not need to worry about which Java layout manager to use. You simply place the control where you want it and move on to placing the next control. How then does Java know where to place the controls?
The answer is in the second class created by Resource Wizard, DialogLayout. The DialogLayout class is created whenever you run Resource Wizard and have one or more dialog resources in the resource template file. The DialogLayout class implements the Java LayoutManager interface. It positions components on a container based on their size and the position in which they were placed in the Dialog Editor.
It is possible to use more than one resource template file to create a single Java applet. In fact, it is a common practice to keep commonly used or shared dialog resources in a file separate from applet-specific dialog resources. When you run Resource Wizard to convert each resource template, you will create multiple copies of the DialogLayout class. If this happens, don't worry about it. You need to add only one copy of DialogLayout to your project workspace and all copies of this class are identical.
The Resource Wizard is a one-way tool. Changes propagate from the resource template file into the generated Java source code. Changes do not flow in the opposite direction. You cannot, for example, edit a Resource Wizard-generated Java file and expect to see the changes appear in the Dialog Editor.
To make a change to a dialog resource, use File | Open to open the resource template file. Make the changes, save the file, and then re-execute the Resource Wizard. If you make changes directly to code generated by Resource Wizard, your changes will be overwritten the next time you run Resource Wizard.
Enough theory, it's time for an example. This section presents an example of using the Dialog Editor to create a dialog resource, running the Resource Wizard to convert the resource into usable Java class code, and then modifying an applet to make use of the new classes. Each of the steps necessary to do this will be described in detail but the following is an overview of the steps necessary to use a dialog resource in an applet:
Example EX07A, described at the end of this section, illustrates how to use Resource Wizard-generated classes to simplify user interface development. Each of these steps will be discussed in relation to how it was performed in creating EX07A. To follow along through each of the steps, use the Applet Wizard to create a new applet. Before generating the applet, turn off comments and multithreading.
First, you need to create a new resource template file. This can be done by selecting New from the File menu and selecting Resource Template. Your project workspace will show the new, empty resource template.
Next, select Resource from the Insert menu. This will display the Insert Resource dialog as shown in Figure 7.2. Select Dialog and press OK to continue.
Figure 7.2 : The Insert Resource dialog.
At this point the Dialog Editor will be displayed and will contain a dialog ready for editing. Your screen should look similar to Figure 7.3.
Figure 7.3 : A new dialog resource ready for editing.
Each dialog resource has a set of properties you can set from within the Dialog Editor. To view the Dialog Properties dialog, double-click on the dialog or press Alt+Enter. You should see a Dialog Properties dialog similar to the one shown in Figure 7.4.
Figure 7.4 : The Dialog Properties dialog.
Here you need to set the ID of the dialog resource you are creating. The ID will be used as the name of the control creator class that Resource Wizard will generate based on this dialog resource. For example, in Figure 7.5 the ID is set to MyControls. This means that a class named MyControls in MyControls.java will be created by the Resource Wizard.
Figure 7.5 : An Edit Box, Static Text, and a Button have been placed on the dialog resource.
Next, place the components on the dialog resource. To do this, select an item in the Controls toolbox and drag it over the dialog. Release it when it is in the proper position. To create example EX07A, drag an Edit Box, a Button, and a List Box onto the resource as shown in Figure 7.5.
As you place each control, set its properties by double-clicking on it. An Edit Box control in the Dialog Editor corresponds to a Java TextField or TextArea. When you place the Edit Box control on the resource, set its ID to NewText, as shown in Figure 7.6. This will be the name given to the member variable in the control creator class.
Figure 7.6 : Setting properties for the Edit Box control.
Similarly, the Button should be given an ID of AddButton and the List Box should be given an ID of ItemList, as shown in Figures 7.7 and 7.8, respectively. After adding these controls, delete the OK and Cancel buttons that were provided by default. When done adding controls, save the resource template. It does not need to be saved in the same directory as your other project files but doing so is usually more convenient. After saving, you can close the Dialog Editor.
Figure 7.7 : Setting properties for the Button control.
Figure 7.8 : Setting properties for the List Box control.
Because the Dialog Editor has its origins in Microsoft's Visual
C++ product it contains controls that cannot be used in Visual
J++. You should be careful to use only controls that can be converted
into Visual J++ components, as shown in Table 7.1.
| Java Component | Dialog Editor Control |
| Button | Button |
| Checkbox (ungrouped) | Check Box |
| Checkbox (grouped) | Radio Button |
| Choice | Combo Box |
| Label | Static text |
| List | List Box |
| Scrollbar | Horizontal or vertical scrollbars |
| TextField | Edit Box (with multiline set to false) |
| TextArea | Edit Box (with multiline set to true) |
By now you have created a new resource template file, added controls to it, and saved the file. You are ready to run the Resource Wizard to convert your work into Java source code. Run the Resource Wizard by selecting Java Resource Wizard from the Tools menu. You will be presented with the first of two Resource Wizard screens, as shown in Figure 7.9. On this screen, enter the name of the resource file you saved in the prior step and select the Next button.
Figure 7.9 : Enter the resource template filename in the first step of the Resource Wizard.
After pressing the Next button you see the second and final page of the Resource Wizard, as shown in Figure 7.10. This screen summarizes the dialog and menu resources that were found in the resource template file. Additionally, it enables you to modify the name of each class that will be generated if you don't like the names you gave on the properties pages in the Dialog Editor.
Figure 7.10 : You can change class names on step 2 of the Resource Wizard.
Selecting Finish will display a summary screen and then generate the necessary classes. In this example, MyControls.java will be generated as a control creator class for the dialog resource you designed and the standard DialogLayout.java will also be generated.
Before you can use the classes that Resource Wizard has generated, you must add them to your project. Do so by selecting Files into Project from the Insert menu.
At this point you're ready to use the generated code. The complete EX07A class is shown in Listing 7.1. The lines of code needed to use the generated code are shown in bold. First, you must use import to make this class aware of the new Java class.
Listing 7.1. EX07A.java.
import java.applet.*;
import java.awt.*;
import MyControls;
public class EX07A extends Applet
{
MyControls ctrls;
public void init()
{
ctrls = new MyControls(this);
ctrls.CreateControls();
}
public boolean action(Event evt, Object obj)
{
if("Add".equals(obj))
{
String str = ctrls.NewText.getText();
ctrls.ItemList.addItem(str);
return true;
}
return false;
}
}
Next, a member instance variable, MyControls ctrls, is declared. This variable is constructed in the init method by passing this to the MyControls constructor. Doing so creates an instance of MyControls that will place its components directly on the applet. This is possible because Applet is a subclass of Container. If, instead, you wanted to place the components on a frame, you could have passed a Frame object to the MyControls constructor. Finally, ctrls.CreateControls is used to actually create the controls.
You should also notice that it was not necessary to use resize to set the size of the applet. Because the control creator class sizes itself to the size of the dialog resource as drawn in the Dialog Editor, it is not necessary to explicitly resize the applet.
The action method is provided to illustrate how to access the individual components. It checks for a push of the Add button. If detected, it gets the text out of the text field named ctrls.NewText and adds this text as an item in the List named ctrls.ItemList. When run, the applet will appear as shown in Figure 7.11.
Figure 7.11 : Example EX07A after adding five items.
In example EX07A you learned how to place Resource Wizard-generated controls onto an applet. You learned that you should add code to the applet's init method to create an instance of the control creator class and then call CreateControls. This works great for placing controls on an applet; however, how do you place controls on a Dialog or Frame because there is no init method in these classes?
Fortunately, placing Resource Wizard-generated controls on a Dialog or Frame is even easier than placing them on an applet. Rather than adding code to the init method of the applet, you can create the controls in the constructor of the class you've derived from Dialog or Frame. This can be seen in example EX07B, shown in Listing 7.2.
Listing 7.2. EX07B.java.
import java.applet.*;
import java.awt.*;
public class EX07B extends Applet
{
public void init()
{
resize(320, 240);
// create a frame for the dialog
// the frame isn't used but is the
// parent of the dialog
Frame frame = new Frame();
// create the dialog and show it
TestDialog dlg = new TestDialog(frame);
dlg.show();
}
}
class TestDialog extends Dialog
{
MyControls ctrls;
public TestDialog(Frame parent)
{
// invoke the Dialog constructor
super(parent, "Test Dialog", false);
// set an appropriate font
setFont(new Font("Dialog", Font.BOLD, 12));
// create the controls made by Resource Wizard
ctrls = new MyControls(this);
ctrls.CreateControls();
}
public boolean action(Event evt, Object arg)
{
boolean result = false;
// when the Close button is pressed, close the dialog
if("Close".equals(evt.arg)) {
dispose();
result = true;
}
return result;
}
}
In this example, the class EX07B extends Applet and creates a Frame to serve as the dialog's parent. Next, an instance of the TestDialog class is created and the show method is used to display it. The TestDialog constructor is where all the action is. This method uses super to construct the dialog, setFont to set an appropriate font for the dialog, and then creates the controls. An action method is provided for the TestDialog class that disposes of the dialog when its Close button is pressed.
It is important that you set the font the dialog will use. Normally
it isn't necessary to set the font. However, the DialogLayout
class generated by Resource Wizard uses the size of the current
font to help determine where to place controls. Because of this,
you should always use setFont when placing Resource Wizard-generated
controls on a Frame or Dialog. For more information
on fonts, see Chapter 16, "Sprucing Things Up with Colors
and Fonts."
| TIP |
Remember, you need to use setFont on a Dialog or Frame that will use a Resource Wizard-generated control creator class. Forgetting to do so will cause the dialog to be displayed without its controls. |
The process of visually designing menus in Visual J++ is completely
analogous to the process you just followed to visually create
and use a dialog resource. Instead of the Dialog Editor you use
the Menu Editor, but after designing and saving menu resources,
you still use the Resource Wizard to generate Java code. Because
menus do not use layout managers, you do not need to use the DialogLayout
class if you use only menus generated by Resource Wizard.
| TIP |
In most cases, you should keep your menu and dialog resources in the same resource template file. Doing so reduces the number of times you will have to run Resource Wizard to generate code. |
Because much of what you'll do to create a menu with the Resource Wizard is similar to what you've already done with dialogs, let's dive right in with an example. This section presents an example of using the Menu Editor to create a menu resource, running the Resource Wizard to convert the resource into usable Java code, and then modifying an applet to make use of the generated code. Each of the steps necessary to do this will be described in detail but the following is an overview of the steps necessary to use a menu resource in an applet:
Example EX07C, which is also included on the CD-ROM, will be built by following these steps. Before getting started, create a new applet with the Applet Wizard. Before generating the applet, turn off comments and multithreading.
After selecting New from the File menu, you will be presented with a list of new file types. Select Resource Template and press OK.
Next, select Resource from the Insert menu. This displays the Insert Resource dialog that was shown in Figure 7.3. Select Menu and press OK to continue. At this point, the Menu Editor will be displayed and will contain a dialog ready for editing. Your screen should look similar to Figure 7.12. In this example, the Menu Editor is shown at the right of the Visual J++ desktop with an empty menu item highlighted and ready to be edited.
Figure 7.12 : A new menu resource ready for editing.
As with dialog resources, you can set the properties of a menu resource. To set the menu's properties, double-click over the menu or press Alt+Enter. The Menu Properties dialog, as shown in Figure 7.13, is displayed. The only property you can set for a menu is its ID. The name you enter here will be the name of the class generated by the Resource Wizard.
Figure 7.13 : The Menu Properties dialog.
In the Menu Editor you start with an empty menu with an outline border where the first menu item should be, as shown in Figure 7.13. You can double-click the mouse in that outline and create a menu item by settings its properties, as shown in Figure 7.15. After you create a menu item on the menu bar, you will have an outline border below the menu bar (indicating a true Java MenuItem) and another outline border on the menu bar adjacent to the first menu name. These can also be seen in Figure 7.14.
Figure 7.14 : Setting the properties of a pop-up menu item.
Figure 7.15 : Setting the properties of a menu item.
In the Menu Editor you can create two types of menu item: a pop-up menu item and a regular menu item. A pop-up menu item does not necessarily pop up; this is a generic term for any menu item that is really the top level of another menu. Therefore, the menu items across a menu bar are pop-up menu items and the items that drop down beneath these are not pop-up menu items. Figure 7.14 illustrated the creation of a pop-up menu item that will appear across the top menu bar. By comparison, Figure 7.15 shows the properties that may be set for a normal menu item.
For all menu items you set a caption that will appear as the text on the screen. For regular menu items you may also specify a value for the ID field. This value will be used to assign a name to the member variable that Resource Wizard will create based on the menu item.
You can create a submenu by indicating that a menu item that is dropped down from the menu bar is a pop-up menu. This is done for the Racquet Sports menu item in Figure 7.16.
Figure 7.16 : Setting pop-up on a menu item creates a submenu.
To create example EX07C , create a Team Sports menu that includes Football and Baseball as menu items. Also, create an Individual Sports menu on the menu bar that includes a Swimming menu item and a Racquet Sports pop-up menu item. Beneath the Racquet Sports submenu create menu items for Tennis and Racquetball.
When finished, save the resource template file.
Select Java Resource Wizard from the Tools menu. Select the newly saved resource template and press Finish to generate your menu class file. For each menu resource in the resource template, the Resource Wizard will create a single Java class. In this example, the menu was named MainMenu, so MainMenu.java will be created.
Select Files into Project from the Insert menu to add the menu class to the project.
The last step is to write code to make use of the generated classes. Listing 7.3 shows the contents of EX07C.java, a class that uses the menu resource that was created during the preceding steps. The lines of code needed to use the generated code are shown in bold. As with all external classes in Java, you must first use import to make this class aware of the new Java class.
Listing 7.3. EX07C.java.
import java.applet.*;
import java.awt.*;
import MainMenu;
public class EX07C extends Applet
{
MainMenu mainMenu;
MenuFrame frame;
public void init()
{
resize(320, 240);
frame = new MenuFrame();
frame.resize(300, 200);
mainMenu = new MainMenu(frame);
mainMenu.CreateMenu();
frame.show();
}
}
class MenuFrame extends Frame
{
String text = new String("You selected: Nothing");
MenuFrame()
{
super("Select a Sport");
}
public void paint(Graphics g)
{
g.drawString(text, 10, 10);
}
public boolean action(Event evt, Object obj)
{
boolean result = false;
if(evt.target instanceof MenuItem)
{
text = "You selected: " + (String)obj;
repaint();
result = true;
}
return result;
}
}
In the EX07C class, two member variables are declared. The first is an instance of the MainMenu class created by Resource Wizard. The second is an instance of a class called MenuFrame. This class is defined later in the file and is necessary because menus cannot be attached directly to an applet. In the init method, the frame is constructed and resized. The variable mainMenu is then constructed as an instance of the MainMenu class. The variable frame is passed to the MainMenu constructor indicating that the menu will be displayed within frame.
Next, mainMenu.CreateMenu is used to create the menu and all its menu items. This method is analogous to the CreateControls method used with dialog resources. Finally, show is used to display the frame.
The MenuFrame class is a simple extension of Frame. Its paint method is used to display the contents of a string and the string is set whenever the user selects a different menu item. The results of executing this applet can be seen in Figure 7.17.
Figure 7.17 : Executing an applet with a menu created by the Resource Wizard.
In this chapter you learned about the Resource Wizard and how it can free you from some of the difficulties of hand-coding your user interface. You learned how the Resource Wizard is a one-way tool that turns your visual designs into Java classes that can be used in your projects. In this chapter you also worked through two examples: one using Resource Wizard with dialog resources and a second using Resource Wizard with menu resources. In the next chapter you will turn your attention to another time-saving feature of Visual J++, the debugger.