List of usage examples for java.awt SystemTray add
public void add(TrayIcon trayIcon) throws AWTException
From source file:com.loy.MainFrame.java
private void systemTray() { if (SystemTray.isSupported()) { ImageIcon icon = new ImageIcon(image); PopupMenu popupMenu = new PopupMenu(); MenuItem itemShow = new MenuItem("Show"); MenuItem itemExit = new MenuItem("Exit"); itemExit.addActionListener(new ActionListener() { @Override//from w ww. j a v a2 s . c o m public void actionPerformed(ActionEvent e) { killProcess(); System.exit(0); } }); itemShow.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { setVisible(true); } }); popupMenu.add(itemShow); popupMenu.add(itemExit); TrayIcon trayIcon = new TrayIcon(icon.getImage(), "E-MICRO-SERVICE-START", popupMenu); SystemTray sysTray = SystemTray.getSystemTray(); try { sysTray.add(trayIcon); } catch (AWTException e1) { } } }
From source file:com.devbury.desktoplib.systemtray.SystemTrayApplication.java
public void start() { logSystemInfo();//from w ww. j av a 2 s. c o m try { if (!isSystemTraySupported()) { JOptionPane.showMessageDialog(null, "Could not start application. This application requires system tray support and your platform " + "does not provide this.", "System Tray Not Supported", JOptionPane.ERROR_MESSAGE); throw new RuntimeException("SystemTray is not supported"); } else { logger.debug("SystemTray is supported"); } if (applicationContext == null) { String[] locations = buildConfigLocations(); logger.debug("Creating context from " + configLocationsToString(locations)); applicationContext = new ClassPathXmlApplicationContext(locations); logger.debug("Context finished building"); } // get the TrayIconDefinition instances Map defs = applicationContext.getBeansOfType(TrayIconDefinition.class); if (defs == null || defs.isEmpty()) { throw new RuntimeException("No TrayIconDefinition instances exist in the context"); } SystemTray tray = newSystemTray(); Iterator<TrayIconDefinition> it = defs.values().iterator(); LinkedList<TrayIcon> installedIcons = new LinkedList<TrayIcon>(); while (it.hasNext()) { TrayIconDefinition def = (TrayIconDefinition) it.next(); try { TrayIcon ti = def.buildTrayIcon(); tray.add(ti); installedIcons.add(ti); } catch (Throwable t) { logger.error("Could not add TrayIconDefinition " + def); } } // get the monitor object out of the context to block on Object monitor = applicationContext.getBean("applicationShutdownService"); // if there was a splash screen shut it down SplashScreen splash = newSplashScreen(); if (splash != null) { logger.debug("Shutting down splash screen"); splash.close(); } synchronized (monitor) { monitor.wait(); } logger.debug("Application shutting down"); Iterator<TrayIcon> trayIconIt = installedIcons.iterator(); while (trayIconIt.hasNext()) { TrayIcon ti = (TrayIcon) trayIconIt.next(); tray.remove(ti); } applicationContext.close(); logger.debug("Application stopped"); } catch (Throwable t) { logger.error("Unresolved exception", t); logger.error("Application shutting down"); if (applicationContext != null) { applicationContext.close(); } logger.error("Application stopped"); } }
From source file:org.kootox.episodesmanager.ui.systray.EpisodesTrayIcon.java
public void create() { //Check the SystemTray support if (!SystemTray.isSupported()) { if (log.isInfoEnabled()) { log.info("SystemTray is not supported"); }/*w ww . j a va2s .c om*/ return; } if (loaded) { return; } final PopupMenu popup = new PopupMenu(); final TrayIcon trayIcon = new TrayIcon(createImage("systray.png", "tray icon")); final SystemTray tray = SystemTray.getSystemTray(); // Create a popup menu components MenuItem display = new MenuItem("Display"); MenuItem exit = new MenuItem("Exit"); //Add components to popup menu popup.add(display); popup.addSeparator(); popup.add(exit); trayIcon.setPopupMenu(popup); try { tray.add(trayIcon); } catch (AWTException e) { if (log.isDebugEnabled()) { log.debug("TrayIcon could not be added."); } return; } trayIcon.addMouseListener(new MouseListener() { @Override public void mouseClicked(MouseEvent mouseEvent) { if (mouseEvent.getButton() == MouseEvent.BUTTON1) { showHide(); } } @Override public void mousePressed(MouseEvent mouseEvent) { //Do nothing } @Override public void mouseReleased(MouseEvent mouseEvent) { //Do nothing } @Override public void mouseEntered(MouseEvent mouseEvent) { //Do nothing } @Override public void mouseExited(MouseEvent mouseEvent) { //Do nothing } }); display.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { showHide(); } }); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { EpisodesManagerMainUI mainUI = EpisodesManagerContext.MAIN_UI_ENTRY_DEF.getContextValue(context); mainUI.close(); } }); loaded = true; if (log.isDebugEnabled()) { log.debug("Systray loaded"); } }
From source file:MonitorSaurausRex.MainMenu.java
private static void createAndShowGUI() { //Check the SystemTray support if (!SystemTray.isSupported()) { System.out.println("SystemTray is not supported"); return;/*from w w w.j a va 2 s. com*/ } final PopupMenu popup = new PopupMenu(); final TrayIcon trayIcon = new TrayIcon(createImage("Logo.png", "tray icon")); final SystemTray tray = SystemTray.getSystemTray(); trayIcon.setImageAutoSize(true); // Create a popup menu components MenuItem aboutItem = new MenuItem("About"); CheckboxMenuItem cb1 = new CheckboxMenuItem("Menu"); CheckboxMenuItem cb2 = new CheckboxMenuItem("Quarantine!"); Menu displayMenu = new Menu("Actions"); MenuItem errorItem = new MenuItem("Cut Connections"); MenuItem warningItem = new MenuItem("Send Reports"); MenuItem infoItem = new MenuItem("Kill Processes"); MenuItem exitItem = new MenuItem("Exit"); //Add components to popup menu popup.add(aboutItem); popup.addSeparator(); popup.add(cb1); popup.add(cb2); popup.addSeparator(); popup.add(displayMenu); displayMenu.add(errorItem); displayMenu.add(warningItem); displayMenu.add(infoItem); popup.add(exitItem); trayIcon.setPopupMenu(popup); try { tray.add(trayIcon); } catch (AWTException e) { System.out.println("TrayIcon could not be added."); return; } trayIcon.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "This dialog box is run from System Tray"); } }); aboutItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "MonitorSaurausRex, Designed for sec203 group project. The program was designed to be piece of software to combat Ransomware" + " in a corparate enviroment. The Project was created by Luke Barlow, Dayan Patel, Rob Shire and Sian Skiggs."); } }); cb1.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { int cb1Id = e.getStateChange(); trayIcon.setImageAutoSize(true); if (cb1Id == ItemEvent.SELECTED) { trayIcon.setImageAutoSize(true); } else { trayIcon.setImageAutoSize(true); } } }); cb2.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { int cb2Id = e.getStateChange(); if (cb2Id == ItemEvent.SELECTED) { trayIcon.setToolTip("Sun TrayIcon"); } else { trayIcon.setToolTip(null); } } }); ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { MenuItem item = (MenuItem) e.getSource(); //TrayIcon.MessageType type = null; System.out.println(item.getLabel()); if ("Error".equals(item.getLabel())) { //type = TrayIcon.MessageType.ERROR; trayIcon.displayMessage("Sun TrayIcon Demo", "This is an error message", TrayIcon.MessageType.ERROR); } else if ("Warning".equals(item.getLabel())) { //type = TrayIcon.MessageType.WARNING; trayIcon.displayMessage("Sun TrayIcon Demo", "This is a warning message", TrayIcon.MessageType.WARNING); } else if ("Info".equals(item.getLabel())) { //type = TrayIcon.MessageType.INFO; trayIcon.displayMessage("Sun TrayIcon Demo", "This is an info message", TrayIcon.MessageType.INFO); } else if ("None".equals(item.getLabel())) { //type = TrayIcon.MessageType.NONE; trayIcon.displayMessage("Sun TrayIcon Demo", "This is an ordinary message", TrayIcon.MessageType.NONE); } } }; errorItem.addActionListener(listener); warningItem.addActionListener(listener); infoItem.addActionListener(listener); exitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { tray.remove(trayIcon); System.exit(0); } }); }
From source file:org.notebook.gui.MainFrame.java
private void installTrayIcon() { visibleTrayIcon = false;/*from w ww.ja va 2 s . co m*/ try { if (java.awt.SystemTray.isSupported()) {// ???? java.awt.SystemTray st = java.awt.SystemTray.getSystemTray(); java.awt.TrayIcon ti = new java.awt.TrayIcon(appIcon16()); ti.setToolTip(this.getTitle()); ti.setPopupMenu(menu.getTrayMenu()); ti.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { events.fireEvent(MenuToolbar.SHOWWINDOW, mainFrame); } }); st.add(ti); visibleTrayIcon = true; } } catch (Exception e) { log.error(e, e); } }
From source file:com.raddle.tools.ClipboardTransferMain.java
public ClipboardTransferMain() { super();// ww w . jav a2s. c o m initGUI(); //// ?? Properties p = new Properties(); File pf = new File(System.getProperty("user.home") + "/clip-trans/conf.properties"); if (pf.exists()) { try { p.load(new FileInputStream(pf)); serverAddrTxt.setText(StringUtils.defaultString(p.getProperty("server.addr"))); portTxt.setText(StringUtils.defaultString(p.getProperty("local.port"))); modifyClipChk.setSelected("true".equals(p.getProperty("allow.modify.local.clip"))); autoChk.setSelected("true".equals(p.getProperty("auto.modify.remote.clip"))); } catch (Exception e) { updateMessage(e.getMessage()); } } m.addListener(new ClipboardListener() { @Override public void contentChanged(Clipboard clipboard) { setRemoteClipboard(false); } }); m.setEnabled(autoChk.isSelected()); // try { pasteImage = ImageIO.read(ClipboardTransferMain.class.getResourceAsStream("/clipboard_paste.png")); grayImage = ImageIO.read(ClipboardTransferMain.class.getResourceAsStream("/clipboard_gray.png")); sendImage = ImageIO.read(ClipboardTransferMain.class.getResourceAsStream("/mail-send.png")); BufferedImage taskImage = ImageIO.read(ClipboardTransferMain.class.getResourceAsStream("/taskbar.png")); setIconImage(taskImage); SystemTray systemTray = SystemTray.getSystemTray(); trayIcon = new TrayIcon(grayImage, "??"); systemTray.add(trayIcon); //// trayIcon.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) {// ??? if (ClipboardTransferMain.this.isVisible()) { ClipboardTransferMain.this.setState(ICONIFIED); } else { ClipboardTransferMain.this.setVisible(true); ClipboardTransferMain.this.setState(NORMAL); } } } }); ////// event this.addWindowListener(new WindowAdapter() { @Override public void windowIconified(WindowEvent e) { ClipboardTransferMain.this.setVisible(false); super.windowIconified(e); } @Override public void windowClosing(WindowEvent e) { File pf = new File(System.getProperty("user.home") + "/clip-trans/conf.properties"); pf.getParentFile().mkdirs(); try { Properties op = new Properties(); op.setProperty("server.addr", serverAddrTxt.getText()); op.setProperty("local.port", portTxt.getText()); op.setProperty("allow.modify.local.clip", modifyClipChk.isSelected() + ""); op.setProperty("auto.modify.remote.clip", autoChk.isSelected() + ""); FileOutputStream os = new FileOutputStream(pf); op.store(os, "clip-trans"); os.flush(); os.close(); } catch (Exception e1) { } shutdown(); super.windowClosing(e); } }); } catch (Exception e) { updateMessage(e.getMessage()); } Thread thread = new Thread() { @Override public void run() { while (true) { try { String poll = iconQueue.take(); if ("send".equals(poll)) { trayIcon.setImage(grayImage); } else if ("paste".equals(poll)) { Thread.sleep(20); trayIcon.setImage(grayImage); } } catch (InterruptedException e1) { return; } } } }; thread.setDaemon(true); thread.start(); }
From source file:de.tbuchloh.kiskis.gui.systray.Java6SystemTray.java
/** * @see de.tbuchloh.kiskis.gui.systray.ISystemTray#show() *///w w w .j a va2 s .co m public void show() { if (!SystemTray.isSupported()) { LOG.error("System tray is not supported!"); return; } final SystemTray tray = SystemTray.getSystemTray(); final Image image = Toolkit.getDefaultToolkit().getImage(_main.getTrayIconURL()); final MouseListener mouseListener = new MouseAdapter() { @Override public void mouseClicked(final MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() >= 2) { _main.setVisible(!_main.isVisible()); } } }; final PopupMenu popup = _main.getPopupMenu(); _trayIcon = new TrayIcon(image, BuildProperties.getFullTitle(), popup); _trayIcon.setImageAutoSize(true); _trayIcon.addMouseListener(mouseListener); try { tray.add(_trayIcon); } catch (final AWTException e) { e.printStackTrace(); } }
From source file:org.kontalk.view.View.java
final void setTray() { if (!Config.getInstance().getBoolean(Config.MAIN_TRAY)) { this.removeTray(); return;// w ww .j a va 2 s . co m } if (!SystemTray.isSupported()) { LOGGER.info("tray icon not supported"); return; } if (mTrayIcon != null) // already set return; // load image Image image = getImage("kontalk.png"); //image = image.getScaledInstance(22, 22, Image.SCALE_SMOOTH); // popup menu outside of frame, officially not supported final WebPopupMenu popup = new WebPopupMenu("Kontalk"); WebMenuItem quitItem = new WebMenuItem(Tr.tr("Quit")); quitItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { View.this.callShutDown(); } }); popup.add(quitItem); // workaround: menu does not disappear when focus is lost final WebDialog hiddenDialog = new WebDialog(); hiddenDialog.setUndecorated(true); // create an action listener to listen for default action executed on the tray icon MouseListener listener = new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { // menu must be shown on mouse release //check(e); } @Override public void mouseReleased(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON1) mMainFrame.toggleState(); else check(e); } private void check(MouseEvent e) { // if (!e.isPopupTrigger()) // return; hiddenDialog.setVisible(true); // TODO ugly code popup.setLocation(e.getX() - 20, e.getY() - 40); popup.setInvoker(hiddenDialog); popup.setCornerWidth(0); popup.setVisible(true); } }; mTrayIcon = new TrayIcon(image, "Kontalk" /*, popup*/); mTrayIcon.setImageAutoSize(true); mTrayIcon.addMouseListener(listener); SystemTray tray = SystemTray.getSystemTray(); try { tray.add(mTrayIcon); } catch (AWTException ex) { LOGGER.log(Level.WARNING, "can't add tray icon", ex); } }
From source file:net.mybox.mybox.ClientGUI.java
private void placeTrayIconAWT() { //Check the SystemTray support if (!SystemTray.isSupported()) { System.out.println("SystemTray is not supported"); return;/*from ww w.j a v a 2s .c om*/ } final PopupMenu popup = new PopupMenu(); final SystemTray tray = SystemTray.getSystemTray(); // Create a popup menu components MenuItem aboutItem = new MenuItem("About Mybox"); MenuItem opendirItem = new MenuItem("Open Directory"); MenuItem prefsItem = new MenuItem("Preferences"); MenuItem exitItem = new MenuItem("Quit Mybox"); //Add components to popup menu popup.add(opendirItem); popup.add(pauseItem); popup.add(syncnowItem); popup.addSeparator(); popup.add(prefsItem); popup.add(aboutItem); popup.add(connectionItem); popup.add(exitItem); trayIcon.setImageAutoSize(true); trayIcon.setToolTip("Mybox"); trayIcon.setPopupMenu(popup); try { tray.add(trayIcon); } catch (AWTException e) { System.out.println("TrayIcon could not be added."); return; } trayIcon.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { launchFileBrowser(); } }); aboutItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //JOptionPane.showMessageDialog(null, "Mybox!"); Dialog dialog = new Dialog((ClientGUI) client.clientGui, "Mybox..."); dialog.setVisible(true); //MessageDialog dialog = new InfoMessageDialog(null, "Mybox", "... is awesome!");//ErrorMessageDialog(null, "Error", message); //dialog.setTitle("About Mybox"); //dialog.run(); //dialog.hide(); } }); prefsItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //window.showAll(); ((ClientGUI) client.clientGui).setVisible(true); } }); syncnowItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { client.FullSync(); } }); opendirItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { launchFileBrowser(); } }); pauseItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (pauseItem.getLabel().equals("Pause Syncing")) { client.pause(); pauseItem.setLabel("Unpause Syncing"); } else if (pauseItem.getLabel().equals("Unpause Syncing")) { client.unpause(); pauseItem.setLabel("Pause Syncing"); } } }); connectionItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (connectionItem.getLabel().equals("Connect")) { client.start(); } else if (connectionItem.getLabel().equals("Disconnect")) { client.stop(); } } }); exitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { tray.remove(trayIcon); System.exit(0); } }); }
From source file:de.mirkosertic.desktopsearch.DesktopSearch.java
@Override public void start(Stage aStage) throws Exception { // This is our base directory File theBaseDirectory = new File(SystemUtils.getUserHome(), "FreeSearchIndexDir"); theBaseDirectory.mkdirs();/* w ww. ja v a2 s .c o m*/ configurationManager = new ConfigurationManager(theBaseDirectory); Notifier theNotifier = new Notifier(); stage = aStage; // Create the known preview processors PreviewProcessor thePreviewProcessor = new PreviewProcessor(); try { // Boot the search backend and set it up for listening to configuration changes backend = new Backend(theNotifier, configurationManager.getConfiguration(), thePreviewProcessor); configurationManager.addChangeListener(backend); // Boot embedded JSP container embeddedWebServer = new FrontendEmbeddedWebServer(aStage, backend, thePreviewProcessor, configurationManager); embeddedWebServer.start(); } catch (BindException | LockReleaseFailedException | LockObtainFailedException e) { // In this case, there is already an instance of DesktopSearch running // Inform the instance to bring it to front end terminate the current process. URL theURL = new URL(FrontendEmbeddedWebServer.getBringToFrontUrl()); // Retrieve the content, but it can be safely ignored // There must only be the get request Object theContent = theURL.getContent(); // Terminate the JVM. The window of the running instance is visible now. System.exit(0); } aStage.setTitle("Free Desktop Search"); aStage.setWidth(800); aStage.setHeight(600); aStage.initStyle(StageStyle.TRANSPARENT); FXMLLoader theLoader = new FXMLLoader(getClass().getResource("/scenes/mainscreen.fxml")); AnchorPane theMainScene = theLoader.load(); final DesktopSearchController theController = theLoader.getController(); theController.configure(this, backend, FrontendEmbeddedWebServer.getSearchUrl(), stage.getOwner()); Undecorator theUndecorator = new Undecorator(stage, theMainScene); theUndecorator.getStylesheets().add("/skin/undecorator.css"); Scene theScene = new Scene(theUndecorator); // Hacky, but works... theUndecorator.setStyle("-fx-background-color: rgba(0, 0, 0, 0);"); theScene.setFill(Color.TRANSPARENT); aStage.setScene(theScene); aStage.getIcons().add(new Image(getClass().getResourceAsStream("/fds.png"))); if (SystemTray.isSupported()) { Platform.setImplicitExit(false); SystemTray theTray = SystemTray.getSystemTray(); // We need to reformat the icon according to the current tray icon dimensions // this depends on the underlying OS java.awt.Image theTrayIconImage = Toolkit.getDefaultToolkit() .getImage(getClass().getResource("/fds_small.png")); int trayIconWidth = new TrayIcon(theTrayIconImage).getSize().width; TrayIcon theTrayIcon = new TrayIcon( theTrayIconImage.getScaledInstance(trayIconWidth, -1, java.awt.Image.SCALE_SMOOTH), "Free Desktop Search"); theTrayIcon.setImageAutoSize(true); theTrayIcon.setToolTip("FXDesktopSearch"); theTrayIcon.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 1) { Platform.runLater(() -> { if (stage.isIconified()) { stage.setIconified(false); } stage.show(); stage.toFront(); }); } } }); theTray.add(theTrayIcon); aStage.setOnCloseRequest(aEvent -> stage.hide()); } else { aStage.setOnCloseRequest(aEvent -> shutdown()); } aStage.setMaximized(true); aStage.show(); }