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:org.yccheok.jstock.gui.MainFrame.java

private void createSystemTrayIcon() {
    if (SystemTray.isSupported()) {
        SystemTray tray = SystemTray.getSystemTray();
        final Image image;
        if (Utils.isWindows7() || Utils.isWindows8()) {
            image = new javax.swing.ImageIcon(getClass().getResource("/images/128x128/chart.png")).getImage();
        } else {//from w  ww. j av  a2s .c om
            image = new javax.swing.ImageIcon(getClass().getResource("/images/16x16/chart.png")).getImage();
        }

        MouseListener mouseListener = new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1) {
                    MainFrame.this.setVisible(true);
                    MainFrame.this.setState(Frame.NORMAL);
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }
        };

        ActionListener exitListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                MainFrame.this.setVisible(false);
                MainFrame.this.dispose();
            }
        };

        PopupMenu popup = new PopupMenu();
        MenuItem defaultItem = new MenuItem(
                java.util.ResourceBundle.getBundle("org/yccheok/jstock/data/gui").getString("MainFrame_Exit"));
        defaultItem.addActionListener(exitListener);
        popup.add(defaultItem);

        trayIcon = new TrayIcon(image, GUIBundle.getString("MainFrame_Application_Title"), popup);

        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        };

        trayIcon.setImageAutoSize(true);
        trayIcon.addActionListener(actionListener);
        trayIcon.addMouseListener(mouseListener);

        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            trayIcon = null;
            JOptionPane.showMessageDialog(MainFrame.this,
                    java.util.ResourceBundle.getBundle("org/yccheok/jstock/data/messages")
                            .getString("warning_message_trayicon_could_not_be_added"),
                    java.util.ResourceBundle.getBundle("org/yccheok/jstock/data/messages").getString(
                            "warning_title_trayicon_could_not_be_added"),
                    JOptionPane.WARNING_MESSAGE);
        }

    } else {
        //  System Tray is not supported
        trayIcon = null;
        JOptionPane.showMessageDialog(MainFrame.this,
                java.util.ResourceBundle.getBundle("org/yccheok/jstock/data/messages")
                        .getString("warning_message_system_tray_is_not_supported"),
                java.util.ResourceBundle.getBundle("org/yccheok/jstock/data/messages")
                        .getString("warning_title_system_tray_is_not_supported"),
                JOptionPane.WARNING_MESSAGE);
    }
}

From source file:org.yccheok.jstock.gui.JStock.java

private void createSystemTrayIcon() {
    if (SystemTray.isSupported()) {
        SystemTray tray = SystemTray.getSystemTray();
        final Image image = new javax.swing.ImageIcon(getClass().getResource("/images/128x128/chart.png"))
                .getImage();/*from   w w w. j  a  v a 2s . c o  m*/

        MouseListener mouseListener = new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1) {
                    JStock.this.setVisible(true);
                    JStock.this.setState(Frame.NORMAL);
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }
        };

        ActionListener exitListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JStock.this.setVisible(false);
                JStock.this.dispose();
            }
        };

        PopupMenu popup = new PopupMenu();
        MenuItem defaultItem = new MenuItem(
                java.util.ResourceBundle.getBundle("org/yccheok/jstock/data/gui").getString("MainFrame_Exit"));
        defaultItem.addActionListener(exitListener);
        popup.add(defaultItem);

        trayIcon = new TrayIcon(image, GUIBundle.getString("MainFrame_Application_Title"), popup);

        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        };

        trayIcon.setImageAutoSize(true);
        trayIcon.addActionListener(actionListener);
        trayIcon.addMouseListener(mouseListener);

        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            trayIcon = null;
            JOptionPane.showMessageDialog(JStock.this,
                    java.util.ResourceBundle.getBundle("org/yccheok/jstock/data/messages")
                            .getString("warning_message_trayicon_could_not_be_added"),
                    java.util.ResourceBundle.getBundle("org/yccheok/jstock/data/messages").getString(
                            "warning_title_trayicon_could_not_be_added"),
                    JOptionPane.WARNING_MESSAGE);
        }

    } else {
        //  System Tray is not supported
        trayIcon = null;
        JOptionPane.showMessageDialog(JStock.this,
                java.util.ResourceBundle.getBundle("org/yccheok/jstock/data/messages")
                        .getString("warning_message_system_tray_is_not_supported"),
                java.util.ResourceBundle.getBundle("org/yccheok/jstock/data/messages")
                        .getString("warning_title_system_tray_is_not_supported"),
                JOptionPane.WARNING_MESSAGE);
    }
}