private static void createAndShowGUI() { if (!SystemTray.isSupported()) { Logger.getLogger(StartAgentGUI.class.getName()).log(Level.SEVERE, "SYSTRAY_NOT_SUPPORTED"); agent.exitProcess(); return; } trayIcon = new TrayIcon( TrayProcess.createImage(AgentConstants.ICON16_PATH, AgentConstants.ICON_DESCRIPTION), AgentConstants.APP_PUBLIC_NAME); final SystemTray tray = SystemTray.getSystemTray(); final AgentPopUpMenu popUpMenu = new AgentPopUpMenu(tray, trayIcon, agent, deviceService); MOUSE_HOOK = new JNIMouseHook(popUpMenu); trayIcon.addMouseListener( new MouseAdapter() { @Override public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger()) { popUpMenu.setLocation(e.getX(), e.getY()); popUpMenu.setInvoker(popUpMenu); popUpMenu.setVisible(true); } } }); popUpMenu.addMouseListener( new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e instanceof MouseClickOutsideComponentEvent) { popUpMenu.setVisible(false); } } }); popUpMenu.addPropertyChangeListener( new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { if (evt.getPropertyName().equals(VISIBLE_PROPERTY)) { if (evt.getNewValue().equals(Boolean.TRUE)) { if (!MOUSE_HOOK.isIsHooked()) { MOUSE_HOOK.setMouseHook(); } } else if (MOUSE_HOOK.isIsHooked()) { MOUSE_HOOK.unsetMouseHook(); } } } }); try { tray.add(trayIcon); } catch (AWTException e) { Logger.getLogger(StartAgentGUI.class.getName()).log(Level.SEVERE, "TRAY_ICON_LOAD_ERROR", e); return; } trayIcon.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog( null, LocalizedLogger.getLocalizedMessage("APPLICATION_INFO", AgentConstants.VERSION)); } }); }