< Prev
Next >



Menu, MenuItems and MenuBar class




In this article, we are going to understand how to add a menu bar, menu and its menu items to the window application.




Simple constructors of MenuBar, Menu and MenuItem


Constructor Description
public MenuBar() Creates a menu bar to which one or many menus are added.
public Menu(String title) Creates a menu with a title.
public MenuItem(String title) Creates a menu item with a title.





Creating a menu, menu items and a submenu.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MenuEx1
{
Frame frame;
MenuBar menuBar;
Menu menu1, menu2; 
MenuItem mItem1, mItem2, mItem3, mItem4, mItem5, mItem6, mItem7;
Button button1, button2, button3;
Dialog d1, d2, d3;




MenuEx1()
{
frame = new Frame("MenuBar, Menu and MenuItems");

//Creating a menu bar
menuBar= new MenuBar();

//Creating first menu
menu1 = new Menu("File");
mItem1 = new MenuItem("New");
mItem2 = new MenuItem("Open");
mItem3 = new MenuItem("Save");

//Adding menu items to the  menu
menu1.add(mItem1);
menu1.add(mItem2);
menu1.add(mItem3);

//Creating a second sub-menu
menu2 = new Menu("Save-as");
mItem5 = new MenuItem(".jpeg");
mItem6 = new MenuItem(".png");
mItem7 = new MenuItem(".pdf");

//Adding menu items to the sub-menu
menu2.add(mItem5);
menu2.add(mItem6);
menu2.add(mItem7);

//Adding the sub-menu to the first menu
menu1.add(menu2);

//Adding our menu  to the menu bar
menuBar.add(menu1);

//Adding my menu bar to the frame by calling setMenuBar() method
frame.setMenuBar(menuBar);

frame.setSize(330,250);
frame.setVisible(true);
}

public static void main(String... ar)
{
new MenuEx1();
}
}

When you run the code, you are presented a window that shows a menu named File

Figure 1


When you click on this menu named File, you are presented its menu items such as New, Open, Save and its sub-menu Save-as and its menu items such as .jpeg, .pdf, .png.

Figure 2





Handling click events on menu items using ActionListener.


import java.awt.*;
import java.awt.event.*;
import java.io.*;


public class MenuEx2 implements ActionListener
{
Frame frame;
MenuBar menuBar;
Menu menu1, menu2; 
MenuItem mItem1, mItem2, mItem3, mItem4, mItem5, mItem6, mItem7;
FileDialog fg;
Label label1;

MenuEx2()
{
frame = new Frame("JDialog");

//Creating a menu bar
menuBar= new 	MenuBar();


//Creating first menu
menu1 = new Menu("File");
mItem1 = new MenuItem("New");
mItem2 = new MenuItem("Open");
mItem3= new MenuItem("Save");
mItem4 = new MenuItem("Exit");


//Adding menu items to the  menu
menu1.add(mItem1);
menu1.add(mItem2);
menu1.add(mItem3);
menu1.add(mItem4);


//Creating a second sub-menu
menu2 = new Menu("Save-as");
mItem5 = new MenuItem(".jpeg");
mItem6 = new MenuItem(".png");
mItem7 = new MenuItem(".pdf");

//Adding menu items to the sub-menu
menu2.add(mItem5);
menu2.add(mItem6);
menu2.add(mItem7);

//Adding the sub-menu to the first menu
menu1.add(menu2);

//Adding the exit menu item at the last of menu
menu1.add(mItem4);


//Adding our menu  to the menu bar
menuBar.add(menu1);

//Adding listener to "Open" and "Exit" menu items
mItem2.addActionListener(this);
mItem4.addActionListener(this);

label1 = new Label("", Label.CENTER);

frame.setMenuBar(menuBar);
frame.add(label1,BorderLayout.CENTER);
frame.setSize(370,270);
frame.setVisible(true);
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Open"))
{

	//Creating an object of FileDialog
	fg = new FileDialog(frame, "Open a file");
	fg.setVisible(true);
	String file = fg.getDirectory()+ fg.getFile();
	label1.setText("File to Open - " + file);
}


if(ae.getActionCommand().equals("Exit"))
{
System.exit(0);
}
}

public static void main(String... ar)
{
new MenuEx2();
}

}

When you run the code, you are presented a window that shows the same menu as we have seen on executing the previous program.

Figure 3


When you click on the menu item named Open, a file chooser dialog window opens us, using which you can choose any file in your computer that you wish to open.


Figure 4

When you select this file, you are presented its path.

Figure 5

In the another response to menu items being clicked, when you click the Exit menu item of our program, the program exits and gets closed.



Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< Frame
Dialog >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2