Example usage for java.awt TrayIcon TrayIcon

List of usage examples for java.awt TrayIcon TrayIcon

Introduction

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

Prototype

public TrayIcon(Image image, String tooltip, PopupMenu popup) 

Source Link

Document

Creates a TrayIcon with the specified image, tooltip and popup menu.

Usage

From source file:com.puzzle.gui.MainFrame.java

public void tray() {//-----ljs
    tray = SystemTray.getSystemTray(); // ?     
    PopupMenu pop = new PopupMenu(); // ????  
    final MenuItem show = new MenuItem("?");
    final MenuItem exit = new MenuItem("?");
    pop.add(show);//from   ww  w . j av a  2  s .  c  om
    pop.add(exit);
    show.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            showMouseClick(e);
        }
    });
    exit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            exitMouseClick(e);
        }
    });

    trayIcon = new TrayIcon(
            Toolkit.getDefaultToolkit().getImage(MainFrame.class.getResource("/images/flag.png")),
            "??V1.0", pop);//  
    trayIcon.setImageAutoSize(true);
    //?  
    trayIcon.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            trayIconMouseClick(e);
        }
    });

    try {
        tray.add(trayIcon); //   
    } catch (AWTException ex) {
        log.error(ex.getMessage(), ex);
        ex.printStackTrace();
    }

}

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 {//  w ww . j  ava  2 s  .c  o m
            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();// w  ww.j a va 2  s  .  c  om

        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);
    }
}