This discussion is archived
7 Replies Latest reply: Sep 30, 2011 10:26 AM by 800557 RSS

Java System Tray Menu- pop up on left click.

821444 Newbie
Currently Being Moderated
Hi,

The system tray menu will normally pop up on mouse right click. I need to make this pop up on mouse left click. Is it possible? I had tried a lot and failed to find a solution. Please help me on this issue.

Thank You.
  • 1. Re: Java System Tray Menu- pop up on left click.
    camickr Expert
    Currently Being Moderated
    The TrayIcon class supports a MouseListener.

    For more help create a [url http://sscce.org]SSCCE, that demonstrates the incorrect behaviour.
  • 2. Re: Java System Tray Menu- pop up on left click.
    821444 Newbie
    Currently Being Moderated
    Hi,

    Thank you for your help,

    I can handle tray icon actions and can set visible any frame windows.

    trayIcon.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    AboutBox eAbout=new AboutBox();
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension screenSize = toolkit.getScreenSize();
    eAbout.setLocation((screenSize.width - 527) / 2, (screenSize.height - 220) / 2);
    eAbout.setVisible(true);
    }
    });

    But i want to pop up the menu itself.

    Thank you,

    Sreejith.
  • 3. Re: Java System Tray Menu- pop up on left click.
    camickr Expert
    Currently Being Moderated
    I thought my reply suggested using a MouseListener. Read the TrayIcon API you can get the popup menu so you should be able to display it yourself on a left mouse click.

    If you have problems the post your SSCCE.

    Please read the "Welcome to the new home" posting at the start of the forum to learn how to use the "code tags" so posted code is formatted and readable.
  • 4. Re: Java System Tray Menu- pop up on left click.
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Moderator action: Moved from Java Programming.

    db
  • 5. Re: Java System Tray Menu- pop up on left click.
    821444 Newbie
    Currently Being Moderated
    Hi,

    Thank you for your answer,

    I had tried a lot to make pop up the system tray menu on mouse left click. Finally i got a way to do this. My code is,
    MouseListener ml;
            ml = new MouseListener() {
                public void mouseClicked(MouseEvent e) {
                    if (mouseflag == 0) {
                        try {
                            Robot robot = new Robot();
                            // RIGHT CLICK
                            robot.mousePress(InputEvent.BUTTON3_MASK);
                            robot.mouseRelease(InputEvent.BUTTON3_MASK);
                        } catch (Exception exe) {
                            System.out.println("error=" + exe);
                        }
                        mouseflag = 1;
                    } else {
                        mouseflag = 0;
                    }
                }
    
            public void mouseEntered(MouseEvent e) {
              System.out.println("Tray icon: Mouse entered");
            }
    
            public void mouseExited(MouseEvent e) {
              System.out.println("Tray icon: Mouse exited");
            }
    
            public void mousePressed(MouseEvent e) {
              System.out.println("Tray icon: Mouse pressed");
            }
    
            public void mouseReleased(MouseEvent e) {
              System.out.println("Tray icon: Mouse released");
            }
          };
    here i had created a mouse right click using code (robot class) on mouse clicked. Now i can pop up menu on left click. But now my issue is, i am not able to remove right click functionality. I think this is in built function for system tray menu. please help or give suggestions on this issue. I think that the way which i used to pop up menu on left click is not a good one. If i removed right click then this code may not work.

    Thank you,

    Sreejith P.M.

    Edited by: Sreeksd on Jan 6, 2011 11:39 PM
  • 6. Re: Java System Tray Menu- pop up on left click.
    821444 Newbie
    Currently Being Moderated
    hi,

    i am new with sun forum. I did not get any answer for my last question. one previous reply was 'Moderator action: Moved from Java Programming.

    db'. what this means? any wrong attempt from my side? please help.

    thank You,

    Sreejith P.M.
  • 7. Re: Java System Tray Menu- pop up on left click.
    800557 Newbie
    Currently Being Moderated
    I have the same issue. I tried to show the PopupMenu manually with popopMenu.show(e.getComponent(),e.getX(),e.getY) but I get a "NullPointerException: parent is null" exception. I've tried to pass as a parameter other components active at the time but I always get that exception. A JPopupMenu runs fine with that same code but I ran into other issues trying to use it instead.

    Any other suggestion?

    Sreeksd, Your code was the closest I got to solving this issue, although it's not an ideal solution. It may not work properly on all systems and on a left handed configured mouse. Why do you want to remove the popup from the right click altogether? If you just want to prevent it from opening twice with the code you posted, you could first check if the left button was clicked and do nothing otherwise. Then the popup will work as expected.

    EDIT: Btw, Darryl Burke's message just means that this thread has been moved to a more appropriate section.

    Edited by: Smigh on Sep 30, 2011 10:25 AM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points