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:
Any time you need to logically divide menus and menu items you can insert a separator.
This helps clarify blocks of options within your menus -- making program easier to use.
To create a separator use the addSeparator() Method.