Creating a Popup Menu : Popup menu « Swing JFC « Java

Creating a Popup Menu


import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

public class Main {
  public static void main(String[] argv) throws Exception {
    final JPopupMenu menu = new JPopupMenu();

    JMenuItem item = new JMenuItem("Item Label");
//  item.addActionListener(actionListener);

    JButton component = new JButton("button");
    component.addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent evt) {
        if (evt.isPopupTrigger()) {
, evt.getX(), evt.getY());

      public void mouseReleased(MouseEvent evt) {
        if (evt.isPopupTrigger()) {
, evt.getX(), evt.getY());



Related examples in the same category

1.A popup menu is sometimes called a context 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