
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create JPopupMenu with Sub-Menu in Java
In this article, we will learn to create a JPopupMenu with a submenu in Java. In general, we can add the menu items to a JPopupMenu and also add a submenu to JPopupMenu by adding the menu items to the submenu first, then adding it to JPopupMenu.
What is a JPopupMenu?
A JPopupMenu is a subclass of JComponent class and it can appear anywhere on the screen when a right mouse button is clicked. In order to create a popup menu, we can use the JPopupMenu class.
Syntax
The following is the syntax for JPopupMenu initialization:
JPopupMenu menu = new JPopupMenu();
A Popup menu is triggered by mouse events, so we need to register a MouseListener interface.
Creating a JPopupMenu with a Submenu
We can override the mouseReleased() method to display the popup menu when we get an appropriate event by calling the isPopupTrigger() method and display it by calling its show() method with the mouse event coordinates as arguments.
The following are the steps for creating a JPopupMenu with a submenu in Java:
Creating the Popup Menu Structure
Creates a new JPopupMenu as "popup", creates a submenu labeled "Course", and adds three items to the submenu: Java, Python, and Scala.
popup = new JPopupMenu(); subMenu = new JMenu("Course"); subMenu.add("Java"); subMenu.add("Python"); subMenu.add("Scala");
Submenu Configuration
Adds two regular menu items, "First Name" and "Last Name", adds a separator line using the addSeparator() method, and adds the submenu we created earlier to the main menu.
popup.add(new JMenuItem("First Name")); popup.add(new JMenuItem("Last Name")); popup.addSeparator(); popup.add(subMenu);
Mouse Listener for Popup Trigger
Adds a mouse listener to detect right-clicks, and then calls the showPopup() method when the mouse is released.
addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent me) { showPopup(me); } });
Popup Logic
Checks if the mouse event is a popup trigger, and shows the popup at the mouse cursor position.
void showPopup(MouseEvent me) { if(me.isPopupTrigger()) popup.show(me.getComponent(), me.getX(), me.getY()); }
Example
Below is an example of creating a JPopupMenu with a submenu in Java:
import java.awt.event.*; import java.awt.*; import javax.swing.*; public class JPopupMenuwithSubMenuTest extends JFrame { private JPopupMenu popup; private JMenu subMenu; public JPopupMenuwithSubMenuTest() { setTitle("JPopupMenuwithSubMenu Test"); popup = new JPopupMenu(); subMenu = new JMenu("Course"); subMenu.add("Java"); subMenu.add("Python"); subMenu.add("Scala"); popup.add(new JMenuItem("First Name")); popup.add(new JMenuItem("Last Name")); popup.addSeparator(); popup.add(subMenu); addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent me) { showPopup(me); } }) ; setSize(400, 275); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } void showPopup(MouseEvent me) { if(me.isPopupTrigger()) popup.show(me.getComponent(), me.getX(), me.getY()); } public static void main(String args[]) { new JPopupMenuwithSubMenuTest(); } }