How to Create Popup Menu in Java Swing

A popup menu is a free-floating menu which associates with an underlying component. This component is called the invoker.  Most of the time, popup menu is linked to a specific component to display context-sensitive choices.

In order to create a popup menu, you use the class JPopupMenu. You then can add menu itemsJMenuItem to popup menu like normal menu. To display the popup menu, you call method show().Normally popup menu is called in response to a mouse event. Here is the code to show the poup menu:

     private void showPopup(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    popup.show(e.getComponent(),
                            e.getX(), e.getY());
                }
            }Code language: JavaScript (javascript)

In the code above, we check to see whether the popup is triggered. If not we pass the component and location where the popup menu is being displayed to the show() method of JPopupMenu instance.

Here is the JPopupMenu demo application screenshot:

Java Popup Menu
package jpopupmenudemo;

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

public class Main {

    public static void main(String[] args) {
        final JFrame frame = new JFrame("Popup Menu Demo");

        // build poup menu
        final JPopupMenu popup = new JPopupMenu();
        // New project menu item
        JMenuItem menuItem = new JMenuItem("New Project...",
                new ImageIcon("images/newproject.png"));
        menuItem.setMnemonic(KeyEvent.VK_P);
        menuItem.getAccessibleContext().setAccessibleDescription(
                "New Project");
        menuItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "New Project clicked!");
            }
        });
        popup.add(menuItem);
        // New File menu item
        menuItem = new JMenuItem("New File...",
                new ImageIcon("images/newfile.png"));
        menuItem.setMnemonic(KeyEvent.VK_F);
        menuItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "New File clicked!");
            }
        });
        popup.add(menuItem);

        // add mouse listener
        frame.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                showPopup(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                showPopup(e);
            }

            private void showPopup(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    popup.show(e.getComponent(),
                            e.getX(), e.getY());
                }
            }
        });

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        frame.setVisible(true);

    }
}Code language: JavaScript (javascript)