Sunday, September 11, 2011

Creating a Sub JMenuItem

Program Description:

The Java Program below demonstrates how to add a JMenu under a JMenu and create a JMenuItem as a sub-menu. "File" is the parent JMenu, "Main Menu" is the child JMenu, and the "Sub Menu 1 - 5" are the JMenuItem. You have to take note that JMenuItem can only be used once in every JMenu so this code won't work:

subItems = new JMenuItem[5];

for(int count=0; count<subItems.length; count++){
subItems[count] = new JMenuItem("Sub Menu "+(count+1));
mainItem[count].add(subItems[count]); //Adding JMenuItem "subItems" in the JMenu "mainItem"
}

this will create an error on line 5 because you are assigning the JMenuItem on every JMenu. The rule is, JMenuItem(s) can only be assigned in to one JMenu so the code goes like this:

subItems = new JMenuItem[5];

for(int count=0; count<subItems.length; count++){
subItems[count] = new JMenuItem("Sub Menu "+(count+1));
mainItem[0].add(subItems[count]); //Adding JMenuItem "subItems" in the JMenu "mainItem"
}

You have to specify a JMenu where the JMenuItem is added.

Output:
Code:

/**
* File: creatingSubJMenuItem.java
* Tiltle: Creating a Sub JMenuItem
* Author: http://java-code-complete.blogspot.com/
*/

//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;

public class creatingSubJMenuItem extends JFrame {

//Initializing program components
private JMenu menus;
private JMenuBar bar;
private JMenu mainItem[];
private JMenuItem subItems[];

//Setting up GUI
public creatingSubJMenuItem() {

//Setting up the Title of the Window
super("Creating a Sub JMenuItem");

//Set Size of the Window (WIDTH, HEIGHT)
setSize(350,200);

//Exit Property of the Window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Constructing JMenu "File" with mnemonic 'F'
menus = new JMenu("File");
menus.setMnemonic('F');

//Constructing main JMenu and add it in JMenu "File"
mainItem = new JMenu[1];

for(int count=0; count<mainItem.length; count++){
mainItem[count] = new JMenu("Main Menu "+(count+1));
menus.add(mainItem[count]); //Adding JMenu "mainItem" in the JMenu "File"
}

//Constructing JMenuItem "subItems" as a Sub Menu to the main JMenu
subItems = new JMenuItem[5];

for(int count=0; count<subItems.length; count++){
subItems[count] = new JMenuItem("Sub Menu "+(count+1));
mainItem[0].add(subItems[count]); //Adding JMenuItem "subItems" in the JMenu "mainItem"
}

//Constructing JMenuBar
bar = new JMenuBar();
bar.add(menus); //Adding the JMenu "File" in the JMenuBar

//Setting up the JMenuBar in the container
setJMenuBar(bar);

//Setting up the container ready for the components to be added.
Container pane = getContentPane();
setContentPane(pane);

/**Set all the Components Visible.
* If it is set to "false", the components in the container will not be visible.
*/
setVisible(true);
}

//Main Method
public static void main (String[] args) {
creatingSubJMenuItem csjmi = new creatingSubJMenuItem();
}
}

Important Part of the Program:

//Constructing JMenu "File" with mnemonic 'F'
menus = new JMenu("File");
menus.setMnemonic('F');

//Constructing main JMenu and add it in JMenu "File"
mainItem = new JMenu[1];

for(int count=0; count<mainItem.length; count++){
mainItem[count] = new JMenu("Main Menu "+(count+1));
menus.add(mainItem[count]); //Adding JMenu "mainItem" in the JMenu "File"
}

//Constructing JMenuItem "subItems" as a Sub Menu to the main JMenu
subItems = new JMenuItem[5];

for(int count=0; count<subItems.length; count++){
subItems[count] = new JMenuItem("Sub Menu "+(count+1));
mainItem[0].add(subItems[count]); //Adding JMenuItem "subItems" in the JMenu "mainItem"
}

//Constructing JMenuBar
bar = new JMenuBar();
bar.add(menus); //Adding the JMenu "File" in the JMenuBar

//Setting up the JMenuBar in the container
setJMenuBar(bar);

No comments:

Post a Comment