trouble with popup menu

Discussion in 'Java' started by Justin, Sep 29, 2006.

  1. Justin

    Justin Guest

    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.
     
    Justin, Sep 29, 2006
    #1
    1. Advertising

  2. Justin

    tiewknvc9 Guest

    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

    Justin wrote:
    > 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, Sep 29, 2006
    #2
    1. Advertising

  3. Justin

    Justin Guest

    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.

    tiewknvc9 wrote:
    > 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
    >
    > Justin wrote:
    > > 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.
     
    Justin, Sep 29, 2006
    #3
  4. Justin

    Justin Guest

    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....
     
    Justin, Sep 29, 2006
    #4
  5. RedGrittyBrick, Oct 1, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9obiBXYWxrZXI=?=

    Popup Window Popup Timing

    =?Utf-8?B?Sm9obiBXYWxrZXI=?=, Nov 4, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    848
    =?Utf-8?B?Sm9obiBXYWxrZXI=?=
    Nov 4, 2005
  2. dapkniht
    Replies:
    1
    Views:
    1,340
    Eliyahu Goldin
    Mar 8, 2006
  3. Perry Loh
    Replies:
    0
    Views:
    302
    Perry Loh
    Mar 24, 2006
  4. Rajesh
    Replies:
    0
    Views:
    561
    Rajesh
    Jul 31, 2007
  5. Jens Peter Hansen
    Replies:
    7
    Views:
    545
    Randy Webb
    Jun 19, 2004
Loading...

Share This Page