next up previous
Next: Menu items Up: Menus Previous: Organizing Menus on the

Creating cascading menus

In many applications it is convenient to organise menu objects in submenus. This technique is known as menu cascading.

Cascading menus (menus added to menus) provide a mechanism to reduce the number of top level menus required in an application, thereby reducing the overall complexity of the user interface.

The program CascadeMenu.java shows how to create a simple cascade menu:

Simple Cascade Menu Example

The code listing for CascadeMenu.java is as follows:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class CascadeMenu
		extends 	JFrame
 {
	private	JPanel		topPanel;
	private	JMenuBar	menuBar;
	private	JMenu		menuFile;
	private	JMenu		menuEdit;
	private	JMenu		menuProperty;

	public CascadeMenu()
	{
		setTitle( "Simle Cascade Menu" );
		setSize( 310, 130 );

		topPanel = new JPanel();
		topPanel.setLayout( new BorderLayout() );
		getContentPane().add( topPanel );

		// Create the menu bar
		menuBar = new JMenuBar();

		// Set this instance as the application's menu bar
		setJMenuBar( menuBar );
		
		// Build the property sub-menu
		menuProperty = new JMenu( "Properties" );
		
		// Create the file menu
		menuFile = new JMenu( "File" );
		menuBar.add( menuFile );

		// Add the property menu
		menuFile.addSeparator();
		menuFile.add( menuProperty );	
		menuFile.addSeparator();

		// Create the file menu
		menuEdit = new JMenu( "Edit" );
		menuBar.add( menuEdit );
	}

	public static void main( String args[] )
	{
		// Create an instance of the test application
		CascadeMenu mainFrame	= new CascadeMenu();
		mainFrame.setVisible( true );
	}
}

The main points to note are:



Dave Marshall
4/14/1999