Popup Menu in Swing

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

public class PopupmenuDemo1 extends JFrame implements ActionListener {
	JPanel p1;
	JPopupMenu popupMenu;
	JMenuItem cutMenuItem, copyMenuItem, pasteMenuItem, findMenuItem;

	public PopupmenuDemo1() {
		super("popup menu");
		setSize(500, 600);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		getContentPane().add(p1 = new JPanel());

		popupMenu = new JPopupMenu("Title");

		// Cut
		cutMenuItem = new JMenuItem("Cut");
		popupMenu.add(cutMenuItem);

		// Copy
		copyMenuItem = new JMenuItem("Copy");
		popupMenu.add(copyMenuItem);
		// Paste
		pasteMenuItem = new JMenuItem("Paste");
		pasteMenuItem.setEnabled(false);
		popupMenu.add(pasteMenuItem);

		// Separator
		popupMenu.addSeparator();

		// Find
		findMenuItem = new JMenuItem("Find");
		popupMenu.add(findMenuItem);
		// add popup menu to the container
		p1.setComponentPopupMenu(popupMenu);

		cutMenuItem.addActionListener(this);
		copyMenuItem.addActionListener(this);
		pasteMenuItem.addActionListener(this);
		findMenuItem.addActionListener(this);
		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() == cutMenuItem) {
			pasteMenuItem.setEnabled(true);
		}
		if (arg0.getSource() == copyMenuItem) {
			pasteMenuItem.setEnabled(true);
		}
	}

	public static void main(String[] args) {

		new PopupmenuDemo1();

	}

}

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