Program Description:
Another fun part in making java program is adding icons in JMenuItem which give more life to your program. The Java program below is a short simple java code that demonstrates on how to add icons in JMenuItem. The required images are:
Place the images inside the folder where your java program is.
Output:
Code:
/** * File: addIconsAndSeparator.java * Tiltle: Adding Icons JMenuItem and Separator on JMenu * Author: http://java-program-sample.blogspot.com/ */ //Java Core Package import javax.swing.*; //Java Extension Package import java.awt.*; public class addIconsAndSeparator 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[]; //Setting up GUI public addIconsAndSeparator() { //Setting up the Title of the Window super("Adding Icons JMenuItem and Separator on JMenu"); //Set Size of the Window (WIDTH, HEIGHT) setSize(400,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[3]; 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); /**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) { addIconsAndSeparator aias = new addIconsAndSeparator(); } }
Important Part of the Program:
//Constructing JMenu, JMenuBar, and JMenuItem fileMenu = new JMenu("File"); menuBar = new JMenuBar(); menuItems = new JMenuItem[3]; 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);