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 8280621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:44:38+00:00 2026-06-08T09:44:38+00:00

I am trying to create a popup on a button through the action listener

  • 0

I am trying to create a popup on a button through the action listener with Java.

I have some code, but I can’t get it to work, though I think I’m close!
This code is from an example but for Pmenu.show, I had to remove the first arg, and I don’t know what to replace it with, which seems to be the problem here.

btnOptions.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                final JPopupMenu Pmenu = new JPopupMenu();
                  JMenuItem menuItem = new JMenuItem("Cut");
                  Pmenu.add(menuItem);
                  menuItem = new JMenuItem("Copy");
                  Pmenu.add(menuItem);
                  menuItem = new JMenuItem("Paste");
                  Pmenu.add(menuItem);
                  menuItem = new JMenuItem("Delete");
                  Pmenu.add(menuItem);
                  menuItem = new JMenuItem("Undo");
                  Pmenu.add(menuItem);
                  Point location = MouseInfo.getPointerInfo().getLocation();
                  Pmenu.show(null, location.getX(), location.getY());
            }
        });
  • 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-06-08T09:44:40+00:00Added an answer on June 8, 2026 at 9:44 am
    Component source = (Component)evt.getSource();
    Point location = MouseInfo.getPointerInfo().getLocation();
    SwingUtilities.convertPointFromScreen(location, source
    Pmenu.show(source, location.getX(), location.getY());
    

    The question that jumps out at me is “why?” Why do it this way? What is it your are trying to achieve?

    UPDATE – Popup offset

    This would display the popup centered horizontally against the source control (the button) and under it.

    Component source = (Component)evt.getSource();
    Point location = source.getLocation();
    Dimension size = source.getSize();
    
    int xPos = location.x + ((size.width - PMenu.getWidth()) / 2;
    int yPos = location.y + size.height;
    Pmenu.show(source, xPos, yPos);
    

    This is, of course, just an example, you would be able to supply your layout information as you please

    WORKING UPDATE

        Component source = (Component)evt.getSource(); 
        Dimension size = source.getSize(); 
    
        int xPos = ((size.width - Pmenu.getPreferredSize().width) / 2); 
        int yPos = size.height;
    
        Pmenu.show(source, xPos, yPos);
    

    Because the popup location is relative to the source, we don’t need the source’s location information

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

Sidebar

Related Questions

I'm trying to create a popup menu, where I can detect the mouse button
I am trying to create a button that can pop up the speech input
I am trying to create a custom popup view that can be called from
I have a simplr svg-graph and I'm trying to create a simple tooltip-popup for
I am trying to create a popup when my link is clicked. I have
I'm trying to create a custom tweet button with a popup, this part works.
I'm trying to create a Gtk popup menu on button click using a handler
I'm trying to find a way to create a popup screen for some user
I'm trying out the new Storyboard feature in iOS 5/XCode, but I can't get
I'm trying to create a simple modal popup. I have the CSS done, and

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.