private static void createAndShowGUI() { // Check the SystemTray support if (!SystemTray.isSupported()) { System.out.println("SystemTray is not supported"); return; } final PopupMenu popup = new PopupMenu(); final TrayIcon trayIcon = new TrayIcon(createImage("logo.gif", "tray icon")); final SystemTray tray = SystemTray.getSystemTray(); // Create a popup menu components MenuItem aboutItem = new MenuItem("About"); MenuItem exitItem = new MenuItem("Exit"); // Add components to popup menu popup.add(aboutItem); popup.addSeparator(); popup.add(exitItem); trayIcon.setPopupMenu(popup); trayIcon.setImageAutoSize(true); trayIcon.setToolTip("Geomar Wetterdaten"); 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, "Windgeschwindigkeit: " + weather.getWindspeed() + "m/s\n" + "Windrichtung: " + weather.getDirection() + "° " + weather.calcDir(weather.getDirection())); } }); exitItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { tray.remove(trayIcon); System.exit(0); } }); }
public void setTray() { if (SystemTray.isSupported()) { Image icon = Toolkit.getDefaultToolkit().getImage("C:/.hack3rClient/sprites/cursors/icon.png"); trayIcon = new TrayIcon(icon, "Vestige-x"); trayIcon.setImageAutoSize(true); try { SystemTray tray = SystemTray.getSystemTray(); tray.add(trayIcon); trayIcon.displayMessage( "Vestige-x", "Vestige-x has been launched!", TrayIcon.MessageType.INFO); PopupMenu menu = new PopupMenu(); final MenuItem minimizeItem = new MenuItem("Hide Vestige-x"); MenuItem BLANK = new MenuItem("-"); MenuItem exitItem = new MenuItem("Quit"); menu.add(minimizeItem); menu.add(BLANK); menu.add(exitItem); trayIcon.setPopupMenu(menu); ActionListener minimizeListener = new ActionListener() { public void actionPerformed(ActionEvent e) { if (frame.isVisible()) { frame.setVisible(false); minimizeItem.setLabel("Show 1# Vestige-x."); } else { frame.setVisible(true); minimizeItem.setLabel("Hide 1# Vestige-x."); } } }; minimizeItem.addActionListener(minimizeListener); ActionListener exitListener = new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }; exitItem.addActionListener(exitListener); } catch (AWTException e) { System.err.println(e); } } }
public static void main(String[] args) { System.out.println(ResourceUsage.getStatus()); final String ipAddress = Util.getIp(); final Thread listener; final SocketListener socketListener = new SocketListener(); listener = new Thread(socketListener); if (!SystemTray.isSupported()) { System.err.println("System tray is not supported."); return; } SystemTray systemTray = SystemTray.getSystemTray(); Image image = Toolkit.getDefaultToolkit().getImage(ServiceDriver.class.getResource("pause.png")); final TrayIcon trayIcon = new TrayIcon(image); final PopupMenu trayPopupMenu = new PopupMenu(); MenuItem startService = new MenuItem("Start Service"); startService.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog( null, "Service Started", "Surrogate Service", JOptionPane.INFORMATION_MESSAGE); try { listener.start(); Image image = Toolkit.getDefaultToolkit() .getImage(ServiceDriver.class.getResource("cyber.gif")); trayIcon.setImage(image); } catch (Exception err) { Image image = Toolkit.getDefaultToolkit() .getImage(ServiceDriver.class.getResource("cyber.gif")); trayIcon.setImage(image); socketListener.resume(); } } }); trayPopupMenu.add(startService); MenuItem action = new MenuItem("Stop Service"); action.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog( null, "Service Stopped", "Surrogate Service", JOptionPane.INFORMATION_MESSAGE); try { socketListener.pause(); Image image = Toolkit.getDefaultToolkit() .getImage(ServiceDriver.class.getResource("pause.png")); trayIcon.setImage(image); } catch (Exception e1) { System.err.println("Service has not stared yet"); } } }); trayPopupMenu.add(action); MenuItem close = new MenuItem("Close"); close.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); trayPopupMenu.add(close); trayIcon.setPopupMenu(trayPopupMenu); trayIcon.addMouseMotionListener( new MouseMotionAdapter() { @Override public void mouseMoved(MouseEvent e) { trayIcon.setToolTip(Util.getStatus(ipAddress)); } }); trayIcon.setImageAutoSize(true); try { systemTray.add(trayIcon); } catch (AWTException awtException) { awtException.printStackTrace(); } }
private static void createAndShowGUI() { // Check the SystemTray support if (!SystemTray.isSupported()) { System.out.println("SystemTray is not supported"); return; } final PopupMenu popup = new PopupMenu(); final TrayIcon trayIcon = new TrayIcon(ImageHelper.loadImage("/images/splash.jpg", "tray icon")); final SystemTray tray = SystemTray.getSystemTray(); // Create a popup menu components MenuItem aboutItem = new MenuItem("About"); CheckboxMenuItem cb1 = new CheckboxMenuItem("Set auto size"); CheckboxMenuItem cb2 = new CheckboxMenuItem("Set tooltip"); Menu displayMenu = new Menu("Display"); MenuItem errorItem = new MenuItem("Error"); MenuItem warningItem = new MenuItem("Warning"); MenuItem infoItem = new MenuItem("Info"); MenuItem noneItem = new MenuItem("None"); 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); displayMenu.add(noneItem); 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, "This dialog box is run from the About menu item"); } }); cb1.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent e) { int cb1Id = e.getStateChange(); if (cb1Id == ItemEvent.SELECTED) { trayIcon.setImageAutoSize(true); } else { trayIcon.setImageAutoSize(false); } } }); 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); noneItem.addActionListener(listener); exitItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { tray.remove(trayIcon); System.exit(0); } }); }