2

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()); } }); 
2
  • For better help sooner, post an SSCCE. Commented Jul 23, 2012 at 3:40
  • Please learn java naming conventions and stick to them. Commented Jul 25, 2012 at 10:53

2 Answers 2

3

try passing in the instance of your window. (this).

According to the documentation, the first parameter is the

invoker - the component in whose space the popup menu is to appear 

So you want to show the popup menu in the window.

Sign up to request clarification or add additional context in comments.

4 Comments

just as a little clarification, the coordinates past to the invoke method are relative to the supplied component. If you supply no component reference, then the coordinates are absolute to the screen
I literally typed in "this" (no quotes) but that didn't work.
@Mad Programmer - I looked at that example and their code saus JDigit.this So I tried btnOptions.this but it tells me to create that class. So I tried another class, which is one of my more "main" class if you will. It was ComicDownloader.this and then .show wants me to remove the arguments.
JPopupMenu.show(Component invoker, int x, int y). You need to pass all three parameters. In your case I'd do something like JPopupMneu.show((Component)e.getSource(), location.getX(), location.getY());
2
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

9 Comments

I have a settings button and when a user clicks it, I want a popup menu to appear. Is that what you're asking?
Yeah, that's fine, just curious
@MichaelScott The main reason I wanted to know, is if you only want to show the popup because of the click, you probably don't need the mouse location. You can use the position of the button (either absolutely on the screen, or relative as we've already pointed out) and display the popup, for example, under the button
Ohhh, I am going to look at all this tonight. What would that code look like?
evt is giving me an error so I had to create a variable just an fyi EventObject evt = null; Then I run the program and click the settings button but I get errors Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at ComicDownloader$6.actionPerformed(ComicDownloader.java:712) Line 712 is this Component source = (Component)evt.getSource();
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.