Search code examples
javauser-interfaceswingusability

Getting JPopupMenu's Position


I am having trouble getting JPopupMenu's position.

What I need is to be able to right click on the work area, click some
menu item, and then create an element in place right under the position of the previously displayed menu.

I tried getting the position of the menu itself and of its items,
but it gives me constant values around 0. (see comments in code)

Because of separation of concerns, the menu is displayed in one class
while its actions are handled in another.

public final class MainFrameMenu
    extends JPopupMenu
    implements ActionListener {

  private final MainFrame mainFrame;
  private final JMenuItem item1 = new JMenuItem("add line");
  private final JMenuItem item2 = new JMenuItem("add element");

  public MainFrameMenu(MainFrame mainFrame) {
    super("Main menu");
    this.mainFrame = mainFrame;

    item1.addActionListener(this);
    item2.addActionListener(this);

    add(item1);
    add(item2);
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == item1) {
      System.out.println(getLocation()); // gives ALWAYS [0, 0]
      System.out.println(item1.getLocation()); // gives ALWAYS [1, 3]

      // I want to create an element IN PLACE
      mainFrame.addConnectionLine(getX(), getY());
    }
  }

}

public final class PopupMouseListener
    extends MouseAdapter {

  private final JPopupMenu menu;

  public PopupMouseListener(JPopupMenu menu) {
    this.menu = menu;
  }

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

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

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

}

I know I could store the last known position in PopupMouseListener, and read it from MainFrameMenu, but it would ruin the classes hierarchy (most other occurrences of PopupMouseListener don't need it).


Solution

  • getLocation() returns the location relative to the parent components coordinate space. This is why item1.getLocation() always returns the same value as it is always in the same location relative to the popup menu.

    From reading the Javadoc it sounds like calling mainFrame.getMousePosition() could return the point you are looking for.