Java AWT MouseAdapter show JPopupMenu on mouse event

Description

Java AWT MouseAdapter show JPopupMenu on mouse event

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.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

public class Main extends JFrame {
   JLabel label = new MyLabel("right-click");

   public Main() {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      getContentPane().add(label);/*from w  ww  .  j  av a  2  s.  c om*/
      pack();
      setSize(300, 100);
   }

   public static void main(String[] args) {
      new Main().setVisible(true);
   }
}
class MyLabel extends JLabel {
   JPopupMenu menu = new JPopupMenu("Popup");
   public MyLabel(String text) {
      super(text);
      addMouseListener(new PopupTriggerListener());

      JMenuItem item = new JMenuItem("Test1");
      item.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            System.out.println("Menu item Test1");
         }
      });
      menu.add(item);

      item = new JMenuItem("Test2");
      item.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            System.out.println("Menu item Test2");
         }
      });
      menu.add(item);
   }

   class PopupTriggerListener extends MouseAdapter {
      public void mousePressed(MouseEvent ev) {
         if (ev.isPopupTrigger()) {
            menu.show(ev.getComponent(), ev.getX(), ev.getY());
         }
      }
      public void mouseReleased(MouseEvent ev) {
         if (ev.isPopupTrigger()) {
            menu.show(ev.getComponent(), ev.getX(), ev.getY());
         }
      }
      public void mouseClicked(MouseEvent ev) {
      }
   }
}



PreviousNext

Related