Adding Action to WindowedApp

An amazing number of things work in WindowedApp, considering that we have yet to write a single line of code. You can enter a file name into the file name Edit control. You can enter and edit text in the multiline Edit control in the middle of the form. You can minimize and maximize the window. You can even close the window by clicking the window's close button. The only things that don't work are the two buttons we added. Of course, the problem is that neither the Forms Designer nor WFC has any idea what these two buttons are for. We still have to define that.

From the Forms Designer, double-click the Cancel button. This opens the Text editor on the newly added method button2_click(). (This method name assumes that button2, the second button we added, is the name of your Cancel button.) Visual J Plus Plus automatically designates the button2_click() method to handle the action event that results from clicking the Cancel button (more on that later in the next section of this chapter).

Edit the button2_click() method so that it looks like the following code:

 * This method is invoked when the user clicks Cancel.
 private void button2_click(Object source, Event e)

The dispose() method closes all open windows in this app and returns their assets to the heap. The call to app.exit() terminates the applica- tion. Rebuild and test the app. Now clicking the Cancel button closes the app.

If you're getting the idea that the Forms Designer has a very "Microsoft Visual Basic feel" about it, you're exactly right. Visual J Plus Plus v6 combines the ease-of-use features of Visual Basic with the C++-like object-oriented Java coding language.

Adding the Submit Action

Of course, the real key to this program's functionality is the Submit button's action handler. Double-click the Submit button to create the button1_click() method. The code for the button1_click() method is similar to the code for the Submit button of the AWT app in , except for differences between the classes and the WFC TextWriter class. Add code to your button1_click() method so that it looks like this:

private void button1_click(Object source, Event e)
 // get the name of the output file
 String fileName = edit1.getText();
 // if there is one…
 if (!fileName.equals(""))
 // open the file with a TextWriter
 TextWriter tw = new TextWriter(fileName, false);
 // get the contents of the text edit field
 // and save to the disk (notice that getLines()
 // returns an array of lines, which must be saved
 // in the file individually)
 String[] array = edit2.getLines();
 for (int i = 0; i < array.length; i++)
 // closing the file automatically flushes the
 // file, plus it ensures that the user can save
 // the contents again in the future

You'll also need to add an import statement at the top of the file to import*. Importing this package will allow the app to gain access to the TextWriter class.

This button1_click method starts by fetching the output file name from the edit1 object using the getText() method. If the file name isn't equal to the null string, the program attempts to create a TextWriter object out of it. The program then calls edit2.getLines(). This method returns an array of String objects, each one representing a different line of text in the edit area.

To write the text lines out to disk, the program enters a for loop that writes out each line of text as long as i is less than the length of the String array. Once the loop is complete, the output to the file is flushed by closing the file before exiting.

Since WFC is written for the Windows environment, it isn't necessary to convert newline characters to newline/carriage return characters as was the case in . This relieves some of the coding burden.

Setting Default Actions

Now that the buttons actually do something, we can assign them to be the default Accept and Cancel buttons. Visual J Plus Plus chooses the Accept button when the user presses the Enter key and the form has focus; it chooses the Cancel button when the user presses the escape key.

Generally you want the Accept button to take some positive action and the Cancel button to bail out without changing anything. The Enter key will not activate the Accept button when focus is inside a multiline edit box.

To set the default Accept and Cancel buttons, select the form and then find the acceptButton property in the Properties window. Clicking the arrow in the property's value box exposes a drop-down menu containing the names of our two buttons button1 and button2 (unless you changed the names during the design process). Select button1—the Submit button. Repeat the process for the cancelButton property except select button2—the Cancel button. Comments