Example usage for java.awt SystemTray isSupported

List of usage examples for java.awt SystemTray isSupported

Introduction

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

Prototype

public static boolean isSupported() 

Source Link

Document

Returns whether the system tray is supported on the current platform.

Usage

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;//w ww  . j  a  v  a  2s.  c o m
    }
    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:com.ethercamp.harmony.desktop.HarmonyDesktop.java

private static void setTrayMenu(TrayIcon trayIcon, MenuItem... items) {
    if (!SystemTray.isSupported()) {
        log.error("System tray is not supported");
        return;/*  ww w.  ja  v a 2  s . c  o m*/
    }
    final SystemTray systemTray = SystemTray.getSystemTray();

    final PopupMenu popupMenu = new PopupMenu();
    stream(items).forEach(i -> popupMenu.add(i));
    trayIcon.setPopupMenu(popupMenu);

    try {
        stream(systemTray.getTrayIcons()).forEach(t -> systemTray.remove(t));
        systemTray.add(trayIcon);
    } catch (AWTException e) {
        log.error("Problem set tray", e);
    }
}

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

/**
 * Setup System Tray Icon.//from ww  w  . ja  va 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:org.nebulaframework.ui.swing.cluster.ClusterMainUI.java

/**
 * System Tray Icon setup/*from  w  ww .  j av  a 2 s .c o m*/
 * @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:de.mycrobase.jcloudapp.Main.java

public void run() {
    if (!SystemTray.isSupported()) {
        showErrorDialog("SystemTray is unsupported!");
        exit();/*w ww  .  j a  v a  2 s.co m*/
    }

    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.kontalk.view.View.java

final void setTray() {
    if (!Config.getInstance().getBoolean(Config.MAIN_TRAY)) {
        this.removeTray();
        return;/*ww  w  .jav  a  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:press.gfw.Windows.java

private void initTray() {

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

    setIconImage(logo);//from  w w  w. ja  v a 2 s.c  o m

    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: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 a va2 s .  com
        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: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 w w .  j  a va  2  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:application.Main.java

private void hide(final Stage stage) {
    Platform.runLater(new Runnable() {
        @Override//from w w w .j  a va 2s  . c o  m
        public void run() {
            // if the operating system
            // supports the system tray
            if (SystemTray.isSupported()) {
                stage.hide();
                showProgramIsMinimizedMsg();
            } else {
                System.exit(0);
            }
        }
    });
}