trouble with popup menu

J

Justin

hello again, havin a bitchin time with this popup menu I'm trying to
create. I'm a learn by doing type of person and I havent found any
good examples of how to implement a pop-up menu. The main class in the
application extends a JPanel. I searched google and found the
following snippet of code and inserted it into my constructor:

final JPopupMenu menu = new JPopupMenu();
// Create and add a menu item
JMenuItem item = new JMenuItem("Item Label");
item.addActionListener(this);
menu.add(item);

// Set the component to show the popup menu
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
if (evt.isPopupTrigger()) {
menu.show(evt.getComponent(), evt.getX(), evt.getY());
}
}
public void mouseReleased(MouseEvent evt) {
if (evt.isPopupTrigger()) {
menu.show(evt.getComponent(), evt.getX(), evt.getY());
}
}
});

however, this code does nothing when I run the application. Currently,
my actionPerformed method is empty, I dont know if I have to add
anything to this method to get the popup to run or not. I'll give it a
try, but any help would be appreciated.
 
T

tiewknvc9

I think that you don't even need that whole mouselistener bit.

try adding the line...

this.setComponentPopupMenu(m_myPopUpMenu);

basically you have created a menu that is not currently being "added"
to anything, so the menu exists, but it is sleeping.

let me know if this worked for you
 
J

Justin

eh, didnt work. I think I got what I needed done however. Instead of
using a Popup, I'm using a JList that appears when a clicked and
disappears when an option has been selected.
 
J

Justin

lol, spoke to soon, that idea worked terrible. If you have any code of
an implemented popup that I could look at, that would be great....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top