Advertisement



< Prev
Next >



JMenu, JMenuItems and JMenuBar 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 JMenuBar, JMenu and JMenuItem


Constructor Description
public JMenuBar() Creates a menu bar to which one or many menus are added.
public JMenu(String title) Creates a menu with a title.
public JMenuItem(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 Menu1
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class Combo


class A implements ActionListener
{
JFrame jf;
JMenuBar menuBar;
JMenu menu1, menu2; 
JMenuItem mItem1, mItem2, mItem3, mItem4, mItem5, mItem6, mItem7;
JButton button1, button2, button3;
JDialog d1, d2, d3;

A()
{
jf = new JFrame("JDialog");

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

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

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

//Creating a second sub-menu
menu2 = new JMenu("Save-as");
mItem5 = new JMenuItem(".jpeg");
mItem6 = new JMenuItem(".png");
mItem7 = new JMenuItem(".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
jf.add(menuBar, BorderLayout.NORTH);

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

}

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



Advertisement




Handling click events on menu items using ActionListener.


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


public class Menu3
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class Combo


class A implements ActionListener
{
JFrame jf;
JMenuBar menuBar;
JMenu menu1, menu2; 
JMenuItem mItem1, mItem2, mItem3, mItem4, mItem5, mItem6, mItem7;
JFileChooser fc;
JLabel label1;

A()
{
jf = new JFrame("JDialog");

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


//Creating first menu
menu1 = new JMenu("File");
mItem1 = new JMenuItem("New");
mItem2 = new JMenuItem("Open");
mItem3= new JMenuItem("Save");
mItem4 = new JMenuItem("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 JMenu("Save-as");
mItem5 = new JMenuItem(".jpeg");
mItem6 = new JMenuItem(".png");
mItem7 = new JMenuItem(".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 JLabel("", JLabel.CENTER);

jf.add(menuBar, BorderLayout.NORTH);
jf.add(label1,BorderLayout.CENTER);
jf.setSize(370,270);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

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

//Creating an object of JFileChooser
fc= new JFileChooser("Open a file");

//Showing a file chooser dialog window
int returnValue = fc.showOpenDialog(jf);

if(returnValue == JFileChooser.APPROVE_OPTION)
{
	File file = fc.getSelectedFile();
	label1.setText(file.getAbsolutePath());
	System.out.println(file.getName());
}

fc.setSize(200,200);
fc.setVisible(true);
}


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

}


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 -





< Prev
Next >
< JFrame
Dialog Boxes >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement