Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 225339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:24:13+00:00 2026-05-11T19:24:13+00:00

I am having trouble getting JPopupMenu ‘s position . What I need is to

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:24:13+00:00Added an answer on May 11, 2026 at 7:24 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I having trouble getting append() to work in safari for some reason, but it
Having trouble getting the following code to work: $('#changeMode').button().click(function(){ $('#playfield').toggle(function() { $(this).switchClass(gridView,plainView); }, function()
Im having trouble getting some html 5 code to work on the android. It
I am having trouble getting routing to work on mono. The default route works
I am having trouble getting a thread to work with OpenCV . The problem
Having trouble getting this syntax right: SELECT DISTINCT id FROM metadata WHERE (meta_key =
Having trouble getting this to work. What's strange is that I have 10 bookmarks
I'm having trouble getting Jquery .closest() to work. Here's my code, it's simple and
I'm having trouble getting the following three-column layout to work: A B C +-------+-------------------+------------+
I'm having trouble getting client validation to work on a web form. I'm including

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.