Complete Menu in Swing

import java.awt.Color;
import java.awt.Event;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;

public class JMenuDemo1 extends JFrame implements ActionListener {
	JMenuBar menuBar;
	JMenu file, edit, view, window, help;
	JMenuItem new1, open, save, close, exit;
	JMenuItem copy, cut, paste, search;
	JRadioButtonMenuItem undo, redo;
	JCheckBoxMenuItem win1, win2, win3;

	JMenu helptype;
	JMenuItem helpWindow, helpHtml;

	ButtonGroup bg = new ButtonGroup();

	Panel1 p1;

	public JMenuDemo1() {
		super();
		setSize(500, 600);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		// adding panel;
		getContentPane().add(p1 = new Panel1());
		setJMenuBar(menuBar = new JMenuBar());

		// file menu
		file = new JMenu(" File ");
		file.setMnemonic(KeyEvent.VK_F);

		file.add(new1 = new JMenuItem("New"));

		new1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
				ActionEvent.ALT_MASK));

		file.add(open = new JMenuItem("Open"));
		file.add(save = new JMenuItem("Save"));
		file.addSeparator();
		file.add(close = new JMenuItem("Close"));
		file.add(exit = new JMenuItem("Exit"));

		// for edit menu
		edit = new JMenu(" Edit ");
		edit.setMnemonic(KeyEvent.VK_E);
		edit.add(copy = new JMenuItem("Copy"));
		// exit.setAccelerator(KeyStroke.getKeyStroke("control alt shift  A"));
		copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
				Event.ALT_MASK + Event.CTRL_MASK));
		edit.add(cut = new JMenuItem("Cut"));
		edit.add(paste = new JMenuItem("Paste"));
		edit.addSeparator();
		edit.add(undo = new JRadioButtonMenuItem("Undo"));
		edit.add(redo = new JRadioButtonMenuItem("Redo"));
		bg.add(undo);
		bg.add(redo);
		edit.addSeparator();
		edit.add(search = new JMenuItem("Search"));

		view = new JMenu(" View ");
		view.setMnemonic(KeyEvent.VK_V);

		window = new JMenu(" Window ");
		window.setMnemonic(KeyEvent.VK_W);
		window.add(win1 = new JCheckBoxMenuItem("Window 1"));
		window.add(win2 = new JCheckBoxMenuItem("Window 2"));
		window.add(win3 = new JCheckBoxMenuItem("Window 3"));

		help = new JMenu("Help ");
		help.setMnemonic(KeyEvent.VK_H);
		helptype = new JMenu("HelpType");
		helpWindow = new JMenuItem("WindowBasedHelp");
		helpHtml = new JMenuItem("HtmlBasedHelp");
		help.add(helptype);
		helptype.add(helpWindow);
		helptype.add(helpHtml);

		menuBar.add(file);
		menuBar.add(edit);
		menuBar.add(view);
		menuBar.add(window);
		menuBar.add(help);

		// for actionlistener
		new1.addActionListener(this);
		open.addActionListener(this);
		save.addActionListener(this);
		close.addActionListener(this);
		exit.addActionListener(this);
		copy.addActionListener(this);
		cut.addActionListener(this);
		paste.addActionListener(this);
		search.addActionListener(this);
		undo.addActionListener(this);
		redo.addActionListener(this);
		win1.addActionListener(this);
		win2.addActionListener(this);
		win3.addActionListener(this);
		setVisible(true);
	}

	public static void main(String a[]) {
		new JMenuDemo1();
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {

		Object obj = arg0.getSource();

		if (obj == new1) {

		}
		if (obj == open) {

		}
		if (obj == save) {

		}
		if (obj == close) {

		}
		if (obj == exit) {
			int i = JOptionPane.showConfirmDialog(null,
					"Do you want to realy close the application",
					"Exit from application", JOptionPane.YES_NO_OPTION);
			if (i == 0)
				System.exit(0);

		}
		if (obj == copy) {

		}
		if (obj == cut) {

		}
		if (obj == paste) {

		}
		if (obj == search) {

		}
		if (obj == undo) {

		}
		if (obj == redo) {

		}
		if (obj == win1) {
			JColorChooser cc = new JColorChooser();
			Color c1 = JColorChooser.showDialog(null, "Select a color",
					new Color(10, 20, 30));
			p1.setBackground(c1);
		}
		if (obj == win2) {

		}

		if (obj == win2) {

		}

	}

	class Panel1 extends JPanel {

		JLabel l1;

		public Panel1() {
			super();
			add(l1 = new JLabel("This is panel"));
			l1.setFont(new Font("Tohama", Font.BOLD, 40));
		}
	}
}

Download

Menu in Swing

Complete Menu example, Menubar,MenuItem etc
Menubar and Menu Item in Awt
Opoup Menu in Swing


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code