Sunday, September 11, 2011

Implementing Event Listener on JMenuItem

Program Description:

Since we already know the basics of JMenuBar, JMenu, and JMenuItem from adding icons, creating Sub-Menus, and combining the three components in to one. We are now going to move on to the event implementation where the menus are going to interact with the user. The Java Program below is a simple code on how to implement and event listener on JMenuItem.

Output:
Code:

/**
* File: JMenuItemEventListener.java
* Tiltle: Implementing Event Listener on JMenuItem
* Author: http://java-code-complete.blogspot.com/
*/

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

public class JMenuItemEventListener extends JFrame {

//Initializing the program Components
private JMenu fileMenu;
private JMenuBar menuBar;
private JMenuItem menuItems[];
private JMenuItem exit;
private String items[] = {"New ...","Open ...","Save ..."};
private char itemMnemonics[] = {'N','O','C','E'};
private String iconFile[] = {"new.gif","open.gif","save.gif"};
private Icon icons[];
private JLabel display;

//Setting up GUI
public JMenuItemEventListener() {

//Setting up the Title of the Window
super("Implementing Event Listener on JMenuItem");

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

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

//Constructing JMenu, JMenuBar, JMenuItem, and JLabel
fileMenu = new JMenu("File");
menuBar = new JMenuBar();
menuItems = new JMenuItem[3];
display = new JLabel("Waiting for Event...", SwingConstants.CENTER); //Display JLabel in the Center

fileMenu.setMnemonic('F'); //Add mnemonic on the JMenu "File"
menuBar.add(fileMenu); //Adding the JMenu to JMenuBar

//Constructing 3 JMenuItem using "for loop"
for(int count=0; count<menuItems.length; count++) {
menuItems[count] = new JMenuItem(items[count],new ImageIcon(iconFile[count])); //Constructing JMenuItem with the Specified String menus and icons
menuItems[count].setMnemonic(itemMnemonics[count]); //Adding mnemonics on JMenuItem
fileMenu.add(menuItems[count]); //Add JMenuItem on JMenu
}

fileMenu.addSeparator(); //Creating a separator to separate exit from New, Open, and Close

exit = new JMenuItem("Exit ..."); //Constructing JMenuItem "Exit"
exit.setMnemonic('E'); //Set JMenuItem "Exit" Mnemonic
fileMenu.add(exit); //Adding JMenuItem "Exit" to 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);

//Implementing Event Listener on JMenuItem[0] which is "New"
menuItems[0].addActionListener(
new ActionListener() {

//Handle JMenuItem event if mouse is clicked.
public void actionPerformed(ActionEvent event) {
//Display this message using JLabel if selected JMenuItem is clicked
display.setText("New Item Menu is Selected");
}
}
);

//Implementing Event Listener on JMenuItem[1] which is "Open"
menuItems[1].addActionListener(
new ActionListener() {

//Handle JMenuItem event if mouse is clicked.
public void actionPerformed(ActionEvent event) {
//Display this message using JLabel if selected JMenuItem is clicked
display.setText("Open Item Menu is Selected");
}
}
);

//Implementing Event Listener on JMenuItem[2] which is "Save"
menuItems[2].addActionListener(
new ActionListener() {

//Handle JMenuItem event if mouse is clicked.
public void actionPerformed(ActionEvent event) {
//Display this message using JLabel if selected JMenuItem is clicked
display.setText("Save Item Menu is Selected");
}
}
);

//Implementing Event Listener on JMenuItem exit
exit.addActionListener(
new ActionListener() {

//Handle JMenuItem event if mouse is clicked.
public void actionPerformed(ActionEvent event) {

System.exit(0); //Exit the Application
}
}
);

//Add JLabel display in the container
pane.add(display, BorderLayout.CENTER);

/**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) {
JMenuItemEventListener jiel = new JMenuItemEventListener();
}
}

Important Part of the Program:

//Constructing JMenu, JMenuBar, JMenuItem, and JLabel
fileMenu = new JMenu("File");
menuBar = new JMenuBar();
menuItems = new JMenuItem[3];
display = new JLabel("Waiting for Event...", SwingConstants.CENTER); //Display JLabel in the Center

fileMenu.setMnemonic('F'); //Add mnemonic on the JMenu "File"
menuBar.add(fileMenu); //Adding the JMenu to JMenuBar

//Constructing 3 JMenuItem using "for loop"
for(int count=0; count<menuItems.length; count++) {
menuItems[count] = new JMenuItem(items[count],new ImageIcon(iconFile[count])); //Constructing JMenuItem with the Specified String menus and icons
menuItems[count].setMnemonic(itemMnemonics[count]); //Adding mnemonics on JMenuItem
fileMenu.add(menuItems[count]); //Add JMenuItem on JMenu
}

fileMenu.addSeparator(); //Creating a separator to separate exit from New, Open, and Close

exit = new JMenuItem("Exit ..."); //Constructing JMenuItem "Exit"
exit.setMnemonic('E'); //Set JMenuItem "Exit" Mnemonic
fileMenu.add(exit); //Adding JMenuItem "Exit" to JMenu

//Setting up the JMenuBar in the container or automtically add JMenuBar in the container
setJMenuBar(menuBar);

//Implementing Event Listener on JMenuItem[0] which is "New"
menuItems[0].addActionListener(
new ActionListener() {

//Handle JMenuItem event if mouse is clicked.
public void actionPerformed(ActionEvent event) {
//Display this message using JLabel if selected JMenuItem is clicked
display.setText("New Item Menu is Selected");
}
}
);

//Implementing Event Listener on JMenuItem[1] which is "Open"
menuItems[1].addActionListener(
new ActionListener() {

//Handle JMenuItem event if mouse is clicked.
public void actionPerformed(ActionEvent event) {
//Display this message using JLabel if selected JMenuItem is clicked
display.setText("Open Item Menu is Selected");
}
}
);

//Implementing Event Listener on JMenuItem[2] which is "Save"
menuItems[2].addActionListener(
new ActionListener() {

//Handle JMenuItem event if mouse is clicked.
public void actionPerformed(ActionEvent event) {
//Display this message using JLabel if selected JMenuItem is clicked
display.setText("Save Item Menu is Selected");
}
}
);

//Implementing Event Listener on JMenuItem exit
exit.addActionListener(
new ActionListener() {

//Handle JMenuItem event if mouse is clicked.
public void actionPerformed(ActionEvent event) {

System.exit(0); //Exit the Application
}
}
);

No comments:

Post a Comment