Home > Software engineering >  How to set location of JPopupMenu relative to system tray icon and when or how to close?
How to set location of JPopupMenu relative to system tray icon and when or how to close?

Time:09-22

I am using JPopupMenu for SystemTray pop up menu but the first problem is how to identify the safest location to place the pop up menu?

The second one How or when to stop display the popup menu? I saw enter image description here

First one, I need some sugessitions to create an algorithm to place the Jpopupmenu in the correct location This one also goin to help to to place undecorated UTILITY JFrame near the Tray Icon.

Second one is how or when the Jpopupmenu shoud be setvisible to false after placing in the location.

CodePudding user response:

You're creating a new JPopupMenu to show on click, but you could instead use the built-in menu for the TrayIcon. It should show in the correct location if you use the PopupMenu instead of the JPopupMenu.

PopupMenu popup = new PopupMenu();
TrayIcon trayIcon = new TrayIcon(createImage("images/bulb.gif", "tray icon"));
MenuItem aboutItem = new MenuItem("About");
popup.add(aboutItem);
trayIcon.setPopupMenu(popup);

https://docs.oracle.com/javase/tutorial/uiswing/misc/systemtray.html

https://docs.oracle.com/javase/7/docs/api/java/awt/TrayIcon.html#setPopupMenu(java.awt.PopupMenu)

  • Related