A popup menu is sometimes called a context menu : Popup menu « Swing JFC « Java






A popup menu is sometimes called a context menu

 


import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

public class PopupMenu {
  public static void main(String[] args) {
    JFrame frame = new JFrame("JPopupMenu");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final Toolkit toolkit = frame.getToolkit();

    final JPopupMenu menu = new JPopupMenu();
    JMenuItem menuItemBeep = new JMenuItem("Beep");

    menuItemBeep.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        toolkit.beep();
      }
    });

    menu.add(menuItemBeep);

    JMenuItem menuItemClose = new JMenuItem("Close");
    menuItemClose.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.exit(0);
      }
    });

    menu.add(menuItemClose);
    frame.addMouseListener(new MouseAdapter() {
      public void mouseReleased(MouseEvent e) {
        if (e.getButton() == e.BUTTON3) {
          menu.show(e.getComponent(), e.getX(), e.getY());
        }
      }
    });

    frame.setSize(250, 200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

   
  








Related examples in the same category

1.Creating a Popup Menu
2.Force the popup menu of a JMenu to be heavyweight
3.Creating a Popup Menu with Nested Menus
4.JPopupMenu with mouse event
5.set Component Popup Menu
6.Forcing a Popup Menu to Be a Heavyweight Component
7.Configure all popup menus to be heavyweight
8.setInheritsPopupMenu