Home Java Example Java Swing Crate a Popup Menu in Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Crate a Popup Menu in Java

Advertisement
Here, you will learn how to create a Popup menu in Java.

Create a Popup Menu in Java

     

Here, you will learn how to create a Popup menu in Java. Popup menu is the list of menu which is displayed at that point on the frame where you press the right mouse button. Following figure shows the popup menu when you right click on the frame:

Pop Up Menu on the Frame

This program illustrates you about the creation of the popup menu. Following methods and APIs have been used in this program for getting popup menu:

JPopupMenu:
This is the class which constructs the popup menu using it's constructor. This class is helpful to add the object of the JMenuItem class which creates a particular menu.

isPopupTrigger():
This is the method of the MouseEvent class of the java.awt.event.*; package. This method returns a boolean type value either true or false. This method returns true if the event is generated when the popup is triggered.

mouseReleased
This is the method of the FormView.MouseEventListener class which is imported from the javax.swing.text.html.*; package. This method receives the generated mouse event when the object is release by clicking the mouse.

getX():
This is the method of the MouseEvent class which is imported from the java.awt.event.*; package. This method returns the integer type value which is the position on the x-axis for the source component where you click the mouse.

getY():
This is also the method of the the MouseEvent class. This method returns the vertical positions of the y-coordinate for the source component where you click the mouse.

show(me.getComponent(), me.getX(), me.getY()):
This is the method of JPopupMenu class which displays the popup menu where you press the right mouse button on the specified location or positions. This positions calculated by the getX() and getY().

getComponent():
This is the method of the ComponentEvent class of the java.awt.event.*; package. This method returns the source component of the generated event.

Here is the code of program:

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

public class PopUpMenu{
  JPopupMenu Pmenu;
  JMenuItem menuItem;
  public static void main(String[] args) {
  PopUpMenu p = new PopUpMenu();
  }

  public PopUpMenu(){
  JFrame frame = new JFrame("Creating a Popup Menu");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  Pmenu = new JPopupMenu();
  menuItem = new JMenuItem("Cut");
  Pmenu.add(menuItem);
  menuItem = new JMenuItem("Copy");
  Pmenu.add(menuItem);
  menuItem = new JMenuItem("Paste");
  Pmenu.add(menuItem);
  menuItem = new JMenuItem("Delete");
  Pmenu.add(menuItem);
  menuItem = new JMenuItem("Undo");
  Pmenu.add(menuItem);
  menuItem.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){}
  });
  frame.addMouseListener(new MouseAdapter(){
  public void mouseReleased(MouseEvent Me){
  if(Me.isPopupTrigger()){
  Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
  }
  }
  });
  frame.setSize(400,400);
  frame.setVisible(true);
  }
}

Download this program

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 14, 2007

Ask Questions?    Discuss: Crate a Popup Menu in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
omprakash
November 10, 2011
not working

C:\Users\OaS\Desktop>javac PopUpMenu error: Class names, 'PopUpMenu', are only accepted if annotation processing is explicitly requested 1 error you have good website ,correct this problem
Hemant Upadhyay
February 22, 2012
Create popup menu

no comment
Varun krishna.P
February 25, 2012
Problem with Jpopup menu

This is not working for me. I'm currently creating an application like note pad. but when i use the code it's not working , it's giving me an error message if i use it with in the main method. Even if i try to use it outside of the main method i'm getting the error message "Cannot refer to a non-final variable pop inside an inner class defined in a different method" .But if i use it with the main method i'm getting the following error message "Syntax error on token "}", delete this token" But i've correctly closed the curly braces. Can you help =me out here ......
MAHESH N. PATEL
April 2, 2012
user

user
Ouro
April 7, 2012
Thank you

This was usefull! Thank you very much
Chetan Nandha
May 3, 2012
Java pop up

Java pop up download for net banking
Petros
June 1, 2012
How to check cases (Cut,Copy etc.)

Hi, I'am totally impressed !it actually worked for me. Thanks. One question of mine would be how can i check for each situation what situation i have and do things then. Let's say that i choose copy from the menu.Then how can i tell to the computer that if copy 'button' was pressed then do that?? Thanx in advance.
PRAMOD KUMAR
June 24, 2012
bobibanking

downlod popup
Jens
November 5, 2012
System compatibility

In order to have the make it compatible for all OS, you need to do it not only in the method mouseReleased, but also in the method mousePressed. This is because it is handled different on MacOS than on Windows and Linux.
Habib
March 10, 2014
How to add action to each menu item!

For each menu item, you should add ActionListener and implement actionPerformed for that specific item. Following can be used for "Cut" menu item: menuItem = new JMenuItem("Cut"); menuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("cut"); } });
DMCA.com