Menus, Dialogs, Windows


This section isn't particularly useful for applet writers.

Menus

Menus in Java must be with the current release of the JDK created with code, since there is no portable resource format for Java, nor a a drag-n-drop menu designer as in AppStudio, Delphi or any tcl/tk GUI builder. There many never be a resource format for Java because of the inherited problems of endian-ness, device independance of icons, bitmaps and availability of fonts. More importantly, there has to be a common, agreed standard everyone must meet...

Note:Rumors of a Windows .RC to AWT in the works.

Albeit Borland and Metrowerks have announced support for Java and knowing Borland fondness of drag-n-drop GUI builders(witness Delphi and Resource Workshop), we can expect one in the near future. It may or may not use a resource format. Symantec who already have a IDE for Java called Espresso may implement one.

For those totally new to programming GUI application with menus, windows, dialogs, I suggest picking up any book on Human-Computer Interaction.

There is no one "good interface" and I won't explain all the details of what makes a good interface, 'good'. That would start a furor of religious wars.

Only Frames can have menus since they implement the MenuContainer interface. Therefore an applet must create a frame in order to use menus. But that just opens a whole can of worms.

The following code is a function that should be called in the constructor of the frame. It is private because we don't want other class to be able to call it.

private void InitializeMenus()
{
	mbar = new MenuBar();
	Menu m = new Menu("File");
	m.add(new MenuItem("New"));
	m.add(new MenuItem("Open"));
	m.add(new MenuItem("Save"));
	m.add(new MenuItem("Save As"));
	m.add(new MenuItem("Print"));
	m.addSeparator();
	m.add(new MenuItem("Quit"));		
	mbar.add(m);
		
	m = new Menu("Help");
	m.add(new MenuItem("Help!!!!"));
	m.addSeparator();
	m.add(new MenuItem("About..."));
	mbar.add(m);

	// Recent change
	setMenuBar(mbar);
}
This is what the menus should look like

The menu events are handled in the same manner as Buttons/Lists/Choices, etc. Event.ACTION_EVENT is called, but instead of Event.target being java.awt.Button, it is now java.awt.MenuItem.

public boolean action(Event evt, Object arg)
{
	// "About..." gets converted to a String and then uses its equals()
	// method to compare with the argument passed to action
	if("About...".equals(arg))
	{
		AboutBox ab = new AboutDialog();
		ab.show();
		return true;
	}
	else if("Ok".equals(arg))
	{
		System.exit(0);
		return true;
	}
	return false;
}

Although checking if the evt.target object is really a MenuItem maybe safer
if(evt.target instanceof MenuItem)
{
	if("About...".equals(arg))
}


Dialogs and Windows

Window

A generic Window that serves little purpose other than to be a parent to other classes. Albeit you could use this class interchangable with Dialog without much loss of functionality. The same can't be said for Frame. It was recommend that this callss be used to implement a pop-up menu. It can, but the JDK beta2 has so many bugs and quirks, don't try.

Here are some of the methods that maybe interesting to you if you are using Windows that are not present in other classes

Dialog

A subclases of Window that can have a border and be modal(i.e prevent the user from doing anything else until he responds). It provides a 'dialog' between the user and your application. Thus its functionality is used for 'About dialog', messages, displaying a list of options, querying for input, etc.
There is still a bug in the JDK/AWT that prevents dialogs from being modal

// Note this class is created hidden
// You'll need to call show() to make it visible
class AboutDialog extends Dialog
{
	static int H_SIZE = 200;
	static int V_SIZE = 200;

	public AboutDialog(Frame parent)
	{
		// Calls the parent telling it this
		// dialog is modal(i.e true)
                super(parent, true);         
		setBackground(Color.gray);
		setLayout(new BorderLayout());
		
		// Two buttons "Close" and "Help"
		Panel p = new Panel();
		p.add(new Button("Close"));
		p.add(new Button("Help"));
                add("South", p);
		resize(H_SIZE, V_SIZE);
	}

        public boolean action(Event evt, Object arg)
        {
		// If action label(i.e arg) equals 
		// "Close" then dispose this dialog
                if(arg.equals("Close"))
                {
                        dispose();
                        return true;
                }
                return false;
        }

	public void paint(Graphics g)
	{
		g.setColor(Color.white);
                g.drawString("My Application", H_SIZE/4, V_SIZE/3);
                g.drawString("Version 1.0", H_SIZE/3, V_SIZE/3+20);      
	}
}
This what the dialog should look like:

Frame

Stand-alone applications should inherit from the class for their main parent window. It also implements the MenuContainer interface and thus can contain menus.

There is no reason to sub-class Frame or use it all, but to provide extra functionality you should. You're probably better off using Frame, instead of its parent: Window. Because Frame implements the MenuContainer interface it has the ability to change the mouse cursor, icon, etc.


Here is an example to put it all in context(This text editor will be documented later)
Notes text editor
[Next] [Prev] [Home]
Nelson Yu
nelson@cs.ualberta.ca
Last modified: Jan 2 1996