Currently, the PopupMenu will show up when I right-click on the TrayIcon in the SystemTray. However, I want it to do the same when I left-click on the TrayIcon.
I thought I might accomplish this by using a mouseListener on the TrayIcon, but I don't know what method to invoke in the mouseClicked event to achieve the desired results.
icon = new TrayIcon(img, tooltip, popup);
icon.addMouseListener(
new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
popup.setEnabled(true);
}
});
Using the setEnabled() method does not make the popup menu appear when I left-click the TrayIcon. It actually has no noticeable effect. I'm wondering what method I should use in the mouseClicked() body in order to make the popup show up when it is left-clicked.
Basically, in your mouse listener, you need to determine which button was pressed (and optional, how many times).
The critical piece of code is this...
I've also included some additional code that makes sure the popup does not cover the task bar and is displayed within the viewable area of the screen (it's a nit pick of mine ;))
What you're trying to do is apparently not possible:
You cannot show the
PopupMenu
with itsshow
method since you need to specify aJComponent
but yourTrayIcon
isn't one (weird enough though thatTrayIcon
still manages to do it, so apparently there is a way, don't ask me though..). So, as MadProgrammer suggested, you should try usingJPopupMenu
instead. Don't add it to yourTrayIcon
for that won't be possible, but display yourJPopupMenu
by adding aMouseListener
to yourTrayIcon
. That should do the trick: