title: Capture Mouse XY Location from the Popup Menu
body: <p>I am able to capture the XY coordinates of both Mouse movements along with where the mouse is Right Clicked to open the Popup Menu.</p>
  
  <p>The issue is how to obtain the XY Coordinates from on top of the Popup Menu in relation to the JFrame. The XY location where the Popup originates is different from the location where the mouse is clicked on the popup.</p>
  
  <p>I tried adding a Mouse Listener to the JPopupMenu object but it never entered the code. </p>
  
  <p>The MousePopupListener looked promising but that did not help either. </p>
  
  <p>The following example is actually 2 examples mashed together. It is supposed to show the XY coordinates as the mouse moves so that the XY is visible during the popup menu but the Layers aren't a strong area and I can only get either the mouse XY to show or the popup Menu to work, but not both. </p>
  
  <p>Anyway, what I would like to do is capture the XY coordinates where the popup menu is invoked (which can be done already) but I also want the XY location relative to the Frame where the mouse is pressed when selecting an item from the Popup menu.</p>
  
  <p>What needs to be added to capture that information?</p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.Graphics;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseMotionAdapter;
  
  import javax.swing.ImageIcon;
  import javax.swing.JComponent;
  import javax.swing.JFrame;
  import javax.swing.JLayeredPane;
  import javax.swing.JMenuItem;
  import javax.swing.JPanel;
  import javax.swing.JPopupMenu;
  import javax.swing.border.BevelBorder;
  import javax.swing.event.PopupMenuEvent;
  import javax.swing.event.PopupMenuListener;
  
  public class MousePopupExample extends JPanel {
  
    public JPopupMenu popup;
  
    public MousePopupExample()
    {
      popup = new JPopupMenu();
      ActionListener menuListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) 
        {
          System.out.println("Popup menu item ["
              + event.getActionCommand() + "] was pressed.");
        }
      };
      JMenuItem item;
      popup.add(item = new JMenuItem("Left", new ImageIcon("1.gif")));
      item.setHorizontalTextPosition(JMenuItem.RIGHT);
      item.addActionListener(menuListener);
      popup.add(item = new JMenuItem("Center", new ImageIcon("2.gif")));
      item.setHorizontalTextPosition(JMenuItem.RIGHT);
      item.addActionListener(menuListener);
      popup.add(item = new JMenuItem("Right", new ImageIcon("3.gif")));
      item.setHorizontalTextPosition(JMenuItem.RIGHT);
      item.addActionListener(menuListener);
      popup.add(item = new JMenuItem("Full", new ImageIcon("4.gif")));
      item.setHorizontalTextPosition(JMenuItem.RIGHT);
      item.addActionListener(menuListener);
      popup.addSeparator();
      popup.add(item = new JMenuItem("Settings . . ."));
      item.addActionListener(menuListener);
  
      popup.setLabel("Justification");
      popup.setBorder(new BevelBorder(BevelBorder.RAISED));
      popup.addPopupMenuListener(new PopupPrintListener());
  
      addMouseListener(new MousePopupListener());
    }
  
  
  // An inner class to check whether mouse events are the popup trigger
    class MousePopupListener extends MouseAdapter 
    {
      public void mouseMoves(MouseEvent e) {
        checkPopup(e);
      }
  
      public void mousePressed(MouseEvent e) {
        checkPopup(e);
      }
  
      public void mouseClicked(MouseEvent e) {
        checkPopup(e);
      }
  
      public void mouseReleased(MouseEvent e) {
        checkPopup(e);
      }
  
      private void checkPopup(MouseEvent e) {
        if (e.isPopupTrigger()) {
          popup.show(MousePopupExample.this, e.getX(), e.getY());
        }
      }
    }
  
    // An inner class to show when popup events occur
    class PopupPrintListener implements PopupMenuListener {
      public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
        System.out.println("Popup menu will be visible!");
      }
  
      public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
        System.out.println("Popup menu will be invisible!");
      }
  
      public void popupMenuCanceled(PopupMenuEvent e) {
        System.out.println("Popup menu is hidden!");
      }
    }
  
    public static void main(String s[]) {
      JFrame frame = new JFrame("Popup Menu Example");
      XYMouseLabel xyMouseLabel = new XYMouseLabel();
  
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      MousePopupExample mpe = new MousePopupExample();
  
      xyMouseLabel.setBounds(0, 0, frame.getWidth(), frame.getHeight());    
      mpe.add(xyMouseLabel);
  
      frame.setContentPane(mpe);
      frame.setPreferredSize(new Dimension(400, 300));
      frame.pack();
  
      frame.addMouseMotionListener(new MouseMotionAdapter() {
          public void mouseMoved(MouseEvent me)
          {
              System.out.println("Mouse Move");
            xyMouseLabel.x = me.getX();
            xyMouseLabel.y = me.getY();
            xyMouseLabel.repaint();
          }
        });
  
      frame.setVisible(true);
    }
  
  
  }
  </code></pre>