Creating a Windows Application with WFC

See Also

Using WFC, you can create Windows applications with the Java programming language. You can also use the RAD features of the Forms Designer to quickly drop controls onto your form, configure their properties, and add event handlers.

Note   Before you use the following procedure to create a Windows application, close any projects that you may already have open. (On the File menu, click Close All.)

To create a Windows application with WFC

  1. On the File menu, click New Project.

  2. On the New tab, expand the Visual J++ Projects folder and click Applications. Then select the Windows Application icon.

  3. In the Name box, enter a name for your project.

  4. In the Location box, enter the path where you want to save your project, or click Browse to navigate to the folder.

  5. Click Open. A collapsed view of your project appears in Project Explorer.

  6. In Project Explorer, expand the project node. A file with the default name of Form1.java has been added to your project.

Note   Renaming this file in Project Explorer does not rename the associated class in the source code, and vice versa. You must manually change all instances of the old name. (Note that you can create an empty project and then add a form with the Form template. This two-step process allows you to name the form before it is created.)

When you use the Windows Application template, your Java class extends com.ms.wfc.ui.Form. To view the source code in the Text editor, right-click Form1.java in Project Explorer and click View Code on the shortcut menu. Program execution begins with the main method. The code in the initForm method represents the modifications you make in the Forms Designer.

Modifying the Form in the Forms Designer

The following procedures show how to add controls to your form in the Forms Designer. This example populates a list box whenever a button is clicked.

To add controls to the form

  1. To open your form in the Forms Designer, double-click Form1.java in Project Explorer.

  2. In the Toolbox, select the WFC Controls tab. (If the Toolbox is not displayed, click Toolbox on the View menu.)

  3. To add a list box to your form, click the ListBox control in the Toolbox and then click the form.

  4. To add a button to your form, click the Button control in the Toolbox and then click the form.

To set the property of a control

  1. Select the Button control on the form to show its properties in the Properties window. (If the Properties window is not displayed, click Properties Window on the View menu.)

  2. Find the button's text property and change the value to Add Item.

To add an event handler

  1. You can also use the Properties window to add handlers to the events of controls. Click the Events toolbar button in the Properties window to display the events for the Button control.

  2. Find the click event and enter addItemClick for the name of the method that will handle the event. When you press ENTER, the Text editor opens to an empty event handler named addItemClick.

  3. Inside the definition of the addItemClick event handler, add the following line of code:

    listBox1.addItem("New string.");

For more information about modifying your code in the development environment, see Editing Code.

Building and Running the Application

When you use the Windows Application template, an .exe file named ProjectName.exe is automatically created when you build your project.

To build and run your application

  1. On the Build menu, click Build. Any compilation errors or messages appear in the Task List. (Double-clicking an error in the Task List moves the insertion point in the Text editor to the error.) Correct the errors and rebuild your application.

  2. To run the application from the development environment, click Start on the Debug menu. To run the application from the command line, simply run the .exe file.

  3. Click the Add Item button. A line of text appears in the list box.

  4. To close the application, click the Windows Close button located in the upper-right corner of the form.

For more information about using the Forms Designer and creating WFC applications, see Designing Forms and Introduction to WFC Programming.