Java Application Tutorial

Java Application Tutorial #3

Now we're into the interesting stuff. We are now at the stage where we can start to add buttons and text boxes to produce something that can react to user input. Here's how the main program (or class) looks:-

import java.awt.*;
import java.io.*;
import java.util.*;

public class TestApp
{
     public static void main(String args[])
     {
	myFrame Window;

       	if (args.length > 0)
	{
             Window=new myFrame(args[0]);
       	}
	else
	{        
     	     Window=new myFrame("Untitled");
	     Window.show();
	     Window.resize(Window.preferredSize());
	}
     }
}

Note that I've called it TestApp again. It would be best to create a seperate subdirectory for this as it will overwrite previous versions. Alternatively, call it another name (remember java is case sensitive) although it can still start to get a little crowded in one folder. We are also still calling a class called myFrame. In a later tutorial, I'll show you how to reuse existing classes you've already created but for the moment we will create the classes from this one file from scratch. On to the class that does all the work which should look a little familiar by now.


class myFrame extends Frame
{
     private Dimension myDimension;
     private Button myButton=new Button("Press me!");
     private TextField myTextField=new TextField(25);

Note here that we are adding a button (at last I hear you cry) and a single line text field (rather than a text area as before). The button is given a caption and the text field is given a width of 25 characters (although it can hold more). They are private variables because they cannot be called by another class and a new version of each of the widgets is created in memory.


     myFrame(String Title)
     {
	super(Title);
	setLayout(new FlowLayout());
	this.add(myTextField);
	this.add(myButton);
        myDimension = new Dimension(200,100);
     }

We set the layout to flow which will put the button and box next to each other but because the width of the window is smaller, it will place them one under the other (unless it is resized by the user - try it when it is running). We then just add them to this frame and we grab the title from the main application (using super). The next three sections we met in previous tutorials.


     public Dimension minimumSize()
     {
	return myDimension;
     }

     public Dimension preferredSize()
     {
	return myDimension;
     }

     public boolean handleEvent(Event ev)
     {
     if (ev.id == Event.WINDOW_DESTROY)
     {
	this.dispose();
	System.exit(0);
        return true;
     }
     return super.handleEvent(ev);
     }

So far so good. Now we come to the bit that reacts to user input. If the button is pressed then the words Hello there! are placed in the text box with alarming originality (not). The event target is an instance of the button (created using the new keyword) which it identifies by the text caption of the button. The text field has it's caption set to the text and that event is returned as having been completed (true). And that's all there is to it. Compile using batch files and be prepared to be amazed.


     public boolean action(Event evt, Object arg)
     {
	if (evt.target instanceof Button)
             if (arg.equals ("Press me!"))
             {
		  myTextField.setText("Hello there!");
                  return true;
             }
	return false;
     }
}

Now note the two last sections. The former is reacting to an event and the second to an action. What's the difference. Generally, an event is more to do with the what the operating system is handling (the closing of a window) whereas an action is more to do with what is happening inside the window. Don't worry, it will become clearer. I think that's enough for the moment. Try changing the captions of the button and text field (remember to change the target text else the event won't fire) and we'll get into something a little meatier next time.

Back to Java Tutor

Written by Nick Cheesman. Last updated: 01/03/2003
Please eMail me at:
nickjc@nickjc.co.uk