Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 890
ivan_ivanovich_ivanoff Avatar asked May 18 '09 16:05

ivan_ivanovich_ivanoff


2 Answers

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.

like image 194
Mark Avatar answered Oct 15 '22 02:10

Mark


Old thread, but I had this issue recently. The problem here is that during the ActionPerformed call, the PopupMenu's visibility has already been set to false, meaning that its location is no longer defined (ie. (0,0)). I am developing for a mouseless interface, so grabbing the mouse's position was not possible for me. I ended up storing the menu's position every time it was made visible and using that to create my component.

like image 35
nfw Avatar answered Oct 15 '22 01:10

nfw