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.
tiewknvc9 - 29 Sep 2006 16:11 GMT
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
> 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
[quoted text clipped - 26 lines]
> anything to this method to get the popup to run or not. I'll give it a
> try, but any help would be appreciated.
Justin - 29 Sep 2006 19:20 GMT
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.
> I think that you don't even need that whole mouselistener bit.
>
[quoted text clipped - 37 lines]
> > anything to this method to get the popup to run or not. I'll give it a
> > try, but any help would be appreciated.
Justin - 29 Sep 2006 19:40 GMT
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....
RedGrittyBrick - 01 Oct 2006 19:21 GMT
> 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....
http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html#popup