Java Application Tutorial

Java Application Tutorial #4

It might be as well to begin building a template or framework on which to base future projects. Most applications that you build will almost certainly involve a menu of some kind and you will want to shut the program down gracefully from a menu so this next program will do just that and we can use it to build upon in future tutorials. We start with the usual main class.

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());
	     // pack() borrowed from 1.1
	     // remove for earlier versions of java
	     Window.pack();
	}
     }
}

The only thing that will change in future will be the contents of the frame which again will form the basis of all future projects. Note the addition of Window.pack() which I have been forced to add. Older versions of Java will still work if this is removed but the frame has to be resized.


class myFrame extends Frame
{
     private Dimension myDimension;

     myFrame(String Title)
     {
	super(Title);
	setLayout(new GridLayout(1,1));

       	// menu's
	MenuBar mb = new MenuBar();
       	Menu m = new Menu("File");
       	m.add(new MenuItem("Exit"));
       	mb.add(m);
       	setMenuBar(mb);

        myDimension = new Dimension(200,200);
     }

The above code starts by creating a new MenuBar object and then adding first a Menu and then a MenuItem. It will look something like this:-

The dimension of the window is kept quite small.

     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;
     	}

     	if (ev.id == Event.ACTION_EVENT)
     	{
		if (ev.target instanceof MenuItem)
		{
			if (ev.arg.equals("Exit"))
			{
		   		System.exit(0);
			}
		}
     	}
     	return super.handleEvent(ev);
     }
}

The final event is of type MenuItem and is waiting for the text Exit at which point the program is closed down. Compile and run it and you'll see a simple window with a menu called File and a single option called Exit which will close the program. Not very useful on the face of it but we can do more by adding another menu and an about box which will pop up with additional information about the application. First, copy all the files from TestMenu into another folder called TestAbout and rename the .java file remembering that it is case sensitive. Next, change the main class name from TestMenu to TestAbout and then the menu adding section so that it looks like this:-

	// menu's	
	MenuBar mb = new MenuBar();
	Menu m = new Menu("File");
	m.add(new MenuItem("Exit"));
	mb.add(m);

	Menu a = new Menu("Help");
	a.add(new MenuItem("About"));
	mb.add(a);

	setMenuBar(mb);

Note how we use a different variable name to refer to the second menu which will be called Help and have a drop down menu called About which we will use to call our new About box. The event handler will now look like this:-

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

     	if (ev.id == Event.ACTION_EVENT)
     	{
		if (ev.target instanceof MenuItem)
		{
			if (ev.arg.equals("Exit"))
			{
		   		System.exit(0);
			}
		}

		if (ev.target instanceof MenuItem)
		{
			if (ev.arg.equals("About"))
			{
				myAbout Window;
				Window=new myAbout("Title");
				Window.show();
                     		Window.resize(Window.preferredSize());
                     		return true;
			}
		}
     	}
     	return super.handleEvent(ev);
     }
}

Note how the About event creates another new frame much like in the main class and it too has a preferred size. Now the class that will create the box.

class myAbout extends Frame
{
     private Dimension myDimension;
     private Button myButton=new Button("OK");
     private TextField myTextField=new TextField("(c) Nick Cheesman 2001",25);

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

     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);
     }

     public boolean action(Event evt, Object arg)
     {
	if (evt.target instanceof Button)
             if (arg.equals ("OK"))
             {
                  this.dispose();
                  return true;
             }
	return false;
     }
}

This new class is a frame with a button and a text box. It responds to the OK button which will close down the frame. It does not respond to any other events apart from if the user closes the window other than using the OK button. The text that appears in the text box is hardcoded for simplicity. Now it's starting to look more like a real application. Try adding a textarea (like a simple text editor) to the main window which is blank at the moment. Until the next time.

Back to Java Tutor

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