Example usage for java.awt SystemTray getSystemTray

List of usage examples for java.awt SystemTray getSystemTray

Introduction

In this page you can find the example usage for java.awt SystemTray getSystemTray.

Prototype

public static SystemTray getSystemTray() 

Source Link

Document

Gets the SystemTray instance that represents the desktop's tray area.

Usage

From source file:com.darkenedsky.reddit.traders.RedditTraders.java

/**
 * Construct a new RedditTraders instance.
 * /*from   w ww  .  j  av a 2s  .co  m*/
 * 
 */
public RedditTraders() {

    // Load XML configuration file, connect to DB and connect to Reddit API
    try {
        config = new Configuration();
        addListener(new Help(this));
        addListener(new About(this));
        addListener(new Confirm(this));
        addListener(new Activate(this));
        listeners.put("DEACTIVATE", new Activate(this));
        addListener(new CountAllSubs(this));
        addListener(new ModHelp(this));
        addListener(new AcceptModInvite(this));
        addListener(new Install(this));
        addListener(new Cake(this));
        addListener(new Trade(this));
        addListener(new TopTraders(this, "TOP20", 20));
        addListener(new Lookup(this));
        addListener(new SetLegacy(this));
        addListener(new SetTextFlair(this));
        addListener(new ViewFlair(this));
        addListener(new Resolve(this));
        listeners.put("BLAME", new Resolve(this));
        listeners.put("CLOSE", new Resolve(this));
        addListener(new SetFlair(this));
        listeners.put("REMOVEFLAIR", new SetFlair(this));
        addListener(new SetModFlair(this));
        listeners.put("REMOVEMODFLAIR", new SetModFlair(this));
        addListener(new SetBlameBan(this));
        addListener(new SetList(this));
        listeners.put("SETHAVELIST", new SetList(this));
        addListener(new GetList(this));
        listeners.put("HAVELIST", new GetList(this));
        addListener(new SetAccountAgeRequirement(this));
        addListener(new SetVerifiedEmail(this));
        addListener(new Undo(this));
        addListener(new LastTrades(this, "LAST10", 10));
        addListener(new SetCheckBan(this));
        addListener(new SetDaysBetween(this));

        // Build a system tray icon
        SystemTray tray = SystemTray.getSystemTray();

        PopupMenu popup = new PopupMenu();

        MenuItem todaysLog = new MenuItem("Today's Log");
        todaysLog.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new TextFrame("logs/RedditTraders.log").setVisible(true);
                } catch (Exception e1) {
                    LOG.error(e1);
                }
            }
        });
        popup.add(todaysLog);

        MenuItem exit = new MenuItem("Exit");
        ActionListener exitListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        };
        exit.addActionListener(exitListener);
        popup.add(exit);

        Image image = Toolkit.getDefaultToolkit().getImage("reddit.png");
        TrayIcon trayIcon = new TrayIcon(image, "RedditTraders " + config.getVersion(), popup);
        trayIcon.setImageAutoSize(true);
        tray.add(trayIcon);

        LOG.debug("RedditTraders launched OK.");
    } catch (Exception x) {
        x.printStackTrace();
        System.exit(0);
    }
}

From source file:org.nebulaframework.ui.swing.node.NodeMainUI.java

/**
 * Setup System Tray Icon./*  w  w w .java 2 s  .  c  o m*/
 * 
 * @param frame owner frame
 */
private void setupTrayIcon(final JFrame frame) {

    idleIcon = Toolkit.getDefaultToolkit()
            .getImage(ClassLoader.getSystemResource("META-INF/resources/node_inactive.png"));

    activeIcon = Toolkit.getDefaultToolkit()
            .getImage(ClassLoader.getSystemResource("META-INF/resources/node_active.png"));

    frame.setIconImage(idleIcon);

    // If system tray is supported by OS
    if (SystemTray.isSupported()) {
        trayIcon = new TrayIcon(idleIcon, "Nebula Grid Node", createTrayPopup());
        trayIcon.setImageAutoSize(true);
        trayIcon.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1) {
                    if (!frame.isVisible()) {
                        frame.setVisible(true);
                    }

                    frame.setExtendedState(JFrame.NORMAL);
                    frame.requestFocus();
                    frame.toFront();
                }
            }

        });

        try {
            SystemTray.getSystemTray().add(trayIcon);
        } catch (AWTException ae) {
            log.debug("[UI] Unable to Initialize Tray Icon");
            return;
        }

        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowIconified(WindowEvent e) {
                // Hide (can be shown using tray icon)
                frame.setVisible(false);
            }

        });
    }

}

From source file:com.piusvelte.taplock.server.TapLockServer.java

private static void initialize() {
    (new File(APP_PATH)).mkdir();
    if (OS == OS_WIN)
        Security.addProvider(new BouncyCastleProvider());
    System.out.println("APP_PATH: " + APP_PATH);
    try {//from w w  w.  j  ava 2 s.co  m
        sLogFileHandler = new FileHandler(sLog);
    } catch (SecurityException e) {
        writeLog("sLogFileHandler init: " + e.getMessage());
    } catch (IOException e) {
        writeLog("sLogFileHandler init: " + e.getMessage());
    }

    File propertiesFile = new File(sProperties);
    if (!propertiesFile.exists()) {
        try {
            propertiesFile.createNewFile();
        } catch (IOException e) {
            writeLog("propertiesFile.createNewFile: " + e.getMessage());
        }
    }

    Properties prop = new Properties();

    try {
        prop.load(new FileInputStream(sProperties));
        if (prop.isEmpty()) {
            prop.setProperty(sPassphraseKey, sPassphrase);
            prop.setProperty(sDisplaySystemTrayKey, Boolean.toString(sDisplaySystemTray));
            prop.setProperty(sDebuggingKey, Boolean.toString(sDebugging));
            prop.store(new FileOutputStream(sProperties), null);
        } else {
            if (prop.containsKey(sPassphraseKey))
                sPassphrase = prop.getProperty(sPassphraseKey);
            else
                prop.setProperty(sPassphraseKey, sPassphrase);
            if (prop.containsKey(sDisplaySystemTrayKey))
                sDisplaySystemTray = Boolean.parseBoolean(prop.getProperty(sDisplaySystemTrayKey));
            else
                prop.setProperty(sDisplaySystemTrayKey, Boolean.toString(sDisplaySystemTray));
            if (prop.containsKey(sDebuggingKey))
                sDebugging = Boolean.parseBoolean(prop.getProperty(sDebuggingKey));
            else
                prop.setProperty(sDebuggingKey, Boolean.toString(sDebugging));
        }
    } catch (FileNotFoundException e) {
        writeLog("prop load: " + e.getMessage());
    } catch (IOException e) {
        writeLog("prop load: " + e.getMessage());
    }

    if (sLogFileHandler != null) {
        sLogger = Logger.getLogger("TapLock");
        sLogger.setUseParentHandlers(false);
        sLogger.addHandler(sLogFileHandler);
        SimpleFormatter sf = new SimpleFormatter();
        sLogFileHandler.setFormatter(sf);
        writeLog("service starting");
    }

    if (sDisplaySystemTray && SystemTray.isSupported()) {
        final SystemTray systemTray = SystemTray.getSystemTray();
        Image trayIconImg = Toolkit.getDefaultToolkit()
                .getImage(TapLockServer.class.getResource("/systemtrayicon.png"));
        final TrayIcon trayIcon = new TrayIcon(trayIconImg, "Tap Lock");
        trayIcon.setImageAutoSize(true);
        PopupMenu popupMenu = new PopupMenu();
        MenuItem aboutItem = new MenuItem("About");
        CheckboxMenuItem toggleSystemTrayIcon = new CheckboxMenuItem("Display Icon in System Tray");
        toggleSystemTrayIcon.setState(sDisplaySystemTray);
        CheckboxMenuItem toggleDebugging = new CheckboxMenuItem("Debugging");
        toggleDebugging.setState(sDebugging);
        MenuItem shutdownItem = new MenuItem("Shutdown Tap Lock Server");
        popupMenu.add(aboutItem);
        popupMenu.add(toggleSystemTrayIcon);
        if (OS == OS_WIN) {
            MenuItem setPasswordItem = new MenuItem("Set password");
            popupMenu.add(setPasswordItem);
            setPasswordItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JPanel panel = new JPanel();
                    JLabel label = new JLabel("Enter your Windows account password:");
                    JPasswordField passField = new JPasswordField(32);
                    panel.add(label);
                    panel.add(passField);
                    String[] options = new String[] { "OK", "Cancel" };
                    int option = JOptionPane.showOptionDialog(null, panel, "Tap Lock", JOptionPane.NO_OPTION,
                            JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
                    if (option == 0) {
                        String password = encryptString(new String(passField.getPassword()));
                        if (password != null) {
                            Properties prop = new Properties();
                            try {
                                prop.load(new FileInputStream(sProperties));
                                prop.setProperty(sPasswordKey, password);
                                prop.store(new FileOutputStream(sProperties), null);
                            } catch (FileNotFoundException e1) {
                                writeLog("prop load: " + e1.getMessage());
                            } catch (IOException e1) {
                                writeLog("prop load: " + e1.getMessage());
                            }
                        }
                    }
                }
            });
        }
        popupMenu.add(toggleDebugging);
        popupMenu.add(shutdownItem);
        trayIcon.setPopupMenu(popupMenu);
        try {
            systemTray.add(trayIcon);
        } catch (AWTException e) {
            writeLog("systemTray.add: " + e.getMessage());
        }
        aboutItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String newline = System.getProperty("line.separator");
                newline += newline;
                JOptionPane.showMessageDialog(null, "Tap Lock" + newline + "Copyright (c) 2012 Bryan Emmanuel"
                        + newline
                        + "This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version."
                        + newline
                        + "This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details."
                        + newline
                        + "You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>."
                        + newline + "Bryan Emmanuel piusvelte@gmail.com");
            }
        });
        toggleSystemTrayIcon.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                setTrayIconDisplay(e.getStateChange() == ItemEvent.SELECTED);
                if (!sDisplaySystemTray)
                    systemTray.remove(trayIcon);
            }
        });
        toggleDebugging.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                setDebugging(e.getStateChange() == ItemEvent.SELECTED);
            }
        });
        shutdownItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                shutdown();
            }
        });
    }
    synchronized (sConnectionThreadLock) {
        (sConnectionThread = new ConnectionThread()).start();
    }
}

From source file:press.gfw.Windows.java

private void initTray() {

    logo = Toolkit.getDefaultToolkit().getImage("logo.png");

    setIconImage(logo);/*from  w  w w.  j  a v  a  2s .c om*/

    if (!SystemTray.isSupported()) {

        return;

    }

    icon = new TrayIcon(logo, null, null);

    icon.setImageAutoSize(true);

    icon.addActionListener(new TrayListener());

    tray = SystemTray.getSystemTray();

    try {

        tray.add(icon);

    } catch (AWTException ex) {

        log("");

        ex.printStackTrace();

    }

}

From source file:org.nebulaframework.ui.swing.cluster.ClusterMainUI.java

/**
 * System Tray Icon setup//  w w  w. j a v a 2  s. c  om
 * @param frame owner JFrame
 */
private void setupTrayIcon(final JFrame frame) {

    // Idle Icon
    idleIcon = Toolkit.getDefaultToolkit()
            .getImage(ClassLoader.getSystemResource("META-INF/resources/cluster_inactive.png"));

    // Active Icon
    activeIcon = Toolkit.getDefaultToolkit()
            .getImage(ClassLoader.getSystemResource("META-INF/resources/cluster_active.png"));

    frame.setIconImage(idleIcon);

    // If system tray is supported by OS
    if (SystemTray.isSupported()) {

        // Set Icon
        trayIcon = new TrayIcon(idleIcon, "Nebula Grid Cluster", createTrayPopup());
        trayIcon.setImageAutoSize(true);
        trayIcon.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1) {
                    if (!frame.isVisible()) {
                        frame.setVisible(true);
                    }

                    frame.setExtendedState(JFrame.NORMAL);
                    frame.requestFocus();
                    frame.toFront();
                }
            }

        });

        try {
            SystemTray.getSystemTray().add(trayIcon);
        } catch (AWTException ae) {
            log.debug("[UI] Unable to Initialize Tray Icon");
            return;
        }

        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowIconified(WindowEvent e) {
                // Hide (can be shown using tray icon)
                frame.setVisible(false);
            }

        });
    }

}

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  av 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:MonitorSaurausRex.MainMenu.java

private static void createAndShowGUI() {
    //Check the SystemTray support
    if (!SystemTray.isSupported()) {
        System.out.println("SystemTray is not supported");
        return;/*from  ww w  . ja  v  a2s  . c om*/
    }
    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.kontalk.view.View.java

final void setTray() {
    if (!Config.getInstance().getBoolean(Config.MAIN_TRAY)) {
        this.removeTray();
        return;/*from  ww w  . j  a  v  a2s .  com*/
    }

    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:de.mycrobase.jcloudapp.Main.java

public void run() {
    if (!SystemTray.isSupported()) {
        showErrorDialog("SystemTray is unsupported!");
        exit();// w w w .  j a  v a2  s  .  c  om
    }

    icon = new TrayIcon(ImageNormal);
    icon.setImageAutoSize(true);
    icon.setToolTip("JCloudApp");
    icon.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!working) {
                working = true;
                doUploadClipboard();
                working = false;
            }
        }
    });

    MenuItem screen = new MenuItem("Take Screenshot");
    screen.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!working) {
                working = true;
                doScreenshot();
                working = false;
            }
        }
    });

    MenuItem uploadClip = new MenuItem("Upload from Clipboard");
    uploadClip.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!working) {
                working = true;
                doUploadClipboard();
                working = false;
            }
        }
    });

    MenuItem upload = new MenuItem("Upload File...");
    upload.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!working) {
                working = true;
                doUploadFile();
                working = false;
            }
        }
    });

    MenuItem about = new MenuItem("About");
    about.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            doAbout();
        }
    });

    MenuItem quit = new MenuItem("Quit");
    quit.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            doQuit();
        }
    });

    PopupMenu popupMenu = new PopupMenu();
    popupMenu.add(screen);
    popupMenu.add(uploadClip);
    popupMenu.add(upload);
    popupMenu.add(about);
    popupMenu.addSeparator();
    popupMenu.add(quit);
    icon.setPopupMenu(popupMenu);

    try {
        SystemTray.getSystemTray().add(icon);
    } catch (AWTException ex) {
        showErrorDialog("No SystemTray found!\n" + ex);
        exit();
    }
}

From source file:org.nebulaframework.ui.swing.cluster.ClusterMainUI.java

/**
 * Removes System Tray icon./*w ww.jav a 2  s .  co m*/
 */
private void removeIcon() {
    if (SystemTray.isSupported()) {
        SystemTray.getSystemTray().remove(trayIcon);
    }
}