Showing posts with label JMenuItem. Show all posts
Showing posts with label JMenuItem. Show all posts

Sunday, September 11, 2011

Creating a JDesktopPane

Program Description:

One of the interesting and useful part in making Java Program is creating a JDesktopPane which is an MDI (Multiple Document Interface) commonly used in today's applications. It is a main window called parent window that contains other windows called child windows. It is used to manage several open documents that are being processed in parallel. The Java Program below is a short simple code on how to create a JDesktopPane and JInternalFrame.

Output:


Code:

/**
 * File: createJDesktoPane.java
 * Tiltle: Creating a JDesktopPane
 * Author: http://java-program-sample.blogspot.com/
 */

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

public class createJDesktoPane extends JFrame {
 
 //Initializing program components
 private JDesktopPane desktopTest;
 private JLabel labels[];
 private JTextField inputs[];
 private JButton buttons[];
 private String labelName[]={"Enter Name: ","Enter Age: ","Enter Address: ","Enter Mobile#: "};
 private String buttonName[] = {"Open","Save","Exit"};
 private JPanel panel1, panel2;

 //Setting up GUI
    public createJDesktoPane() {
     
     //Setting up the Title of the Window
     super("Creating a JDesktopPane");

     //Set Size of the Window (WIDTH, HEIGHT)
     setSize(600,500);

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

     JMenuBar bar = new JMenuBar(); //Constructing JMenuBar
     JMenu menu = new JMenu("File"); //Constructing JMenu name "File"
     JMenuItem newFile = new JMenuItem("Add New Data"); //Constructing JMenuItem with "Add New Data" label
     
     menu.add(newFile); //Adding JMenuItem in the JMenu
     bar.add(menu); //Adding JMenu in the JMenuBar
     
  setJMenuBar(bar); //Adding JMenuBar in the container
  
  desktopTest = new JDesktopPane(); //Creating a JDesktopPane
  desktopTest.setBackground(Color.BLACK); //Setting JDesktopPane background color
  
     //Setting up the container ready for the components to be added.
     Container pane = getContentPane();
     setContentPane(pane);
     
     pane.add(desktopTest); //Adding JDesktopPane in the container
     
     //Implemeting Even-Listener on newFile JMenuItem
     newFile.addActionListener(
  new ActionListener() {
   
   //Handle JMenuItem "newFile" event if it is clicked
   public void actionPerformed(ActionEvent e) {
   
   //Constructing an Internal Frame inside JDesktopPane
   JInternalFrame frame = new JInternalFrame(null,true,true,true,true);
   
   Container container = frame.getContentPane(); //Creating a container inside the JInternalFrame
   
   //Constructing JLabel, JButton, and JTextField inside JInternalFrame
   labels = new JLabel[4];
   inputs = new JTextField[4];
   buttons = new JButton[3];
   
   //Creating a JPanel 1 with GridLayout of 4 rows and 2 columns inside JInternalFrame
   panel1 = new JPanel();
   panel1.setLayout(new GridLayout(4,2));
   
   //Constructing JLabel and JTextField using "for loop" and add to JPanel 1
   for(int count=0; count<labels.length && count<inputs.length; count++) {
    labels[count] = new JLabel(labelName[count]);
    inputs[count] = new JTextField(10);
    panel1.add(labels[count]);
    panel1.add(inputs[count]);
   }
   
   //Creating a JPanel 2 with GridLayout of 1 row and 3 columns inside JInternalFrame
   panel2 = new JPanel();
   panel2.setLayout(new GridLayout(1,3));
   
   //Constructing JButton using "for loop" and add to JPanel 2
   for(int count=0; count<buttons.length; count++) {
    buttons[count] = new JButton(buttonName[count]);
    panel2.add(buttons[count]);
   }
   
   //Adding JPanel 1 and 2 to the JInternalFrame container
   container.add(panel1,BorderLayout.NORTH);
   container.add(panel2,BorderLayout.CENTER);
   
   frame.setTitle("Add New Data"); //Set the Title of the JInternalFrame
   frame.setResizable(false); //Lock the size of the JInternalFrame
   frame.setMaximizable(false); //Disable the Maximize function of JInternalFrame
   
   //Set the size of JInternalFrame to the size of its content
   frame.pack();
   
   //Attached the JInternalFrame to JDesktopPane and show it by setting the visible in to "true"
   desktopTest.add(frame);
   frame.setVisible(true);
   }
  }
  );

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

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-program-sample.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
   }
  }
  );

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-program-sample.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);

Friday, September 9, 2011

Adding Icons JMenuItem and Separator on JMenu

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);

Adding JMenuItem on JMenu

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