我在系统托盘中添加了一个托盘图标。弹出菜单与托盘图标相关联。根据托盘图标上设置的图像,应显示弹出菜单。例如:右键单击TrayIcon
上的灰色图像,应显示活动菜单;对于任何其他图像,应该发出停用菜单。
它在大多数系统中都按预期工作,但在某些系统中,弹出菜单并不是偶然出现的。例如:右键单击TrayIcon
上的灰色图像获取非活动菜单而不是激活菜单。我想如果我们继续右键单击托盘图标,弹出菜单就不会刷新。有没有办法刷新托盘图标中的弹出菜单?
我正在使用trayIcon.setPopupMenu(popupMenu);
非常感谢任何帮助。
public void handleRightClick(MouseEvent event)throws Exception{
LOGGER.info("Mouse right click happens on tray icon..");
CustomTrayIcon cTray = new CustomTrayIcon();
CIcon icon = new CIcon();
PopupMenu popupMenu=null;
String popupName = null;
if (trayIcon != null) {
if (trayIcon.getImage() == icon.createGrayIcon()) {
popupMenu = cTray.getActivatePopupMenu();
if(trayIcon.getPopupMenu() == null){
popupName = "Name Not Set";
}
else{
popupName = trayIcon.getPopupMenu().getName();
}
LOGGER.info("Before setting activate Popup menu into tray icon. Name:" + popupName);
popupMenu.setName("Active Menu");
trayIcon.setPopupMenu(popupMenu);
LOGGER.info("After setting activate Popup menu into tray icon. Name: " + trayIcon.getPopupMenu().getName());
} else {
popupMenu = cTray.getDeactivatePopupMenu();
if(trayIcon.getPopupMenu() == null){
popupName = "Name Not Set";
}
else{
popupName = trayIcon.getPopupMenu().getName();
}
LOGGER.info("Before setting deactivate Popup menu into tray icon. Name:" + popupName);
popupMenu.setName("Deactive Menu");
trayIcon.setPopupMenu(popupMenu);
LOGGER.info("After setting deactivate Popup menu into tray icon. Name: " + trayIcon.getPopupMenu().getName());
}
}
}