A basic WFC form is a public class extending the Form class with a default constructor and an initForm method. When the Form class is instantiated, the class constructor calls the initForm method, which is where the Forms Designer puts all the code used to initialize the form and control properties. Other code specific to your application follows the call to initForm in the constructor. In the MyNotepad application, the title for the application is set here (although it could have just as well been set in the form's Properties window). The constructor for the MyNotepad application is:
public MyNotepad()
{
// Required for Visual J++ Form Designer support
initForm();
this.setBounds(100, 100, 300, 300);
this.setText("Untitled - MyNotepad");
}
The Visual J++ Forms Designer adds declarations for any added controls in the main body of the class just before the initForm method. For example, here are the declarations for the objects that make up the MyNotepad.java form:
/**
* NOTE: The following code is required by the Visual J++ Forms
* Designer. It can be modified using the Form editor. Do not
* modify it using the Text editor.
*/
Container components = new Container();
MainMenu Menu = new MainMenu();
MenuItem FileMenu = new MenuItem();
MenuItem FileMenuNew = new MenuItem();
MenuItem FileMenuOpen = new MenuItem();
MenuItem FileMenuSave = new MenuItem();
MenuItem FileMenuSaveAs = new MenuItem();
MenuItem FileMenuExit = new MenuItem();
MenuItem HelpMenu = new MenuItem();
MenuItem HelpMenuAbout = new MenuItem();
Edit editbox = new Edit();
private void initForm()
{
. . .
The Visual J++ Forms Designer creates this declaration code as well as the code in the initForm method that sets properties of the form and the controls placed on the form. The infrastructure for handling events is also tightly integrated with the Forms Designer, which can generate event handler mappings in the initForm method.
The first two statements in the initForm method demonstrate how the Forms Designer sets properties on an object (in this case setting the menu item Text property to “&New”) and uses the object’s addOnClick method to establish a click event handler for the object.
private void initForm()
{
FileMenuNew.setText("&New");
FileMenuNew.addOnClick(new EventHandler(this.FileMenuNew_click));