Friday, September 9, 2011

Adding JMenuItem on JMenu

Design Free Website with Free Internet Marketing Tools

Program Description:

The program below is a simple short java code on how to add a JMenuItem in a JMenu. JMenuItem is a drop-down menu component inside JMenu where you add Icons and implement Event Listener.

Output:
Code:

/**
 * File: addJMenuItemOnJMenu.java
 * Tiltle: Adding JMenuItem on JMenu
 * Author: http://java-program-sample.blogspot.com/
 */

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

public class addJMenuItemOnJMenu extends JFrame {
 
 //Initializing JMenu, JMenuBar, JMenuItem, specified JMenuItem strings, and JMenuItem Mnemonics
 private JMenu fileMenu;
 private JMenuBar menuBar;
 private JMenuItem menuItems[];
 private String items[] = {"New ...","Open ...","Close ...","Exit ..."};
 private char itemMnemonics[] = {'N','O','C','E'};

 //Setting up GUI
    public addJMenuItemOnJMenu() {
     
     //Setting up the Title of the Window
     super("Adding JMenuItem on JMenu");

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

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

  //Constructing JMenu, JMenuBar, and JMenuItem
     fileMenu = new JMenu("File");
     menuBar = new JMenuBar();
     menuItems = new JMenuItem[4];
     
     fileMenu.setMnemonic('F'); //Add mnemonic on the JMenu "File"
     menuBar.add(fileMenu); //Adding JMenu on JMenuBar
     
     //Constructing JMenuItem using "for loop"
     for(int count=0; count<menuItems.length; count++) {
      menuItems[count] = new JMenuItem(items[count]); //Constructing JMenuItem with the Specified String menus
      menuItems[count].setMnemonic(itemMnemonics[count]); //Adding mnemonics on JMenuItem
      fileMenu.add(menuItems[count]); //Add JMenuItem on JMenu
     }
     
     //Setting up the JMenuBar in the container or automtically add JMenuBar in the container
     setJMenuBar(menuBar);
     
     //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) {
     addJMenuItemOnJMenu ajmi = new addJMenuItemOnJMenu();
 }
}

Important Part of the Program:

//Constructing JMenu, JMenuBar, and JMenuItem
     fileMenu = new JMenu("File");
     menuBar = new JMenuBar();
     menuItems = new JMenuItem[4];
     
     fileMenu.setMnemonic('F'); //Add mnemonic on the JMenu "File"
     menuBar.add(fileMenu); //Adding JMenu on JMenuBar
     
     //Constructing JMenuItem using "for loop"
     for(int count=0; count<menuItems.length; count++) {
      menuItems[count] = new JMenuItem(items[count]); //Constructing JMenuItem with the Specified String menus
      menuItems[count].setMnemonic(itemMnemonics[count]); //Adding mnemonics on JMenuItem
      fileMenu.add(menuItems[count]); //Add JMenuItem on JMenu
     }
     
     //Setting up the JMenuBar in the container or automtically add JMenuBar in the container
     setJMenuBar(menuBar);

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Hostgator Discount Code