{"id":906,"date":"2019-02-17T18:12:55","date_gmt":"2019-02-17T09:12:55","guid":{"rendered":"https:\/\/chat-messenger.com\/?p=906"},"modified":"2019-07-03T00:05:55","modified_gmt":"2019-07-02T15:05:55","slug":"trayicon-blink","status":"publish","type":"post","link":"https:\/\/chat-messenger.com\/en\/blog\/java\/trayicon-blink","title":{"rendered":"Java Tasktray Icons Displayed and Blinking"},"content":{"rendered":"<h2><a name=\"0\">Java.<strong>task tray<\/strong>(in Japanese history)<strong>system tray (e.g. in Microsoft Windows)<\/strong>) icons.<strong>Indication\/Flashing<\/strong><\/a><\/h2>\n<p>Java.<strong>task tray<\/strong>(in Japanese history)<strong>system tray (e.g. in Microsoft Windows)<\/strong>This section introduces utility classes that display and blink the icons of the<\/p>\n<p>In the utility class, the new feature added in Java SE 6 Mustang is <strong>SystemTray<\/strong> , ,<strong>TrayIcon<\/strong> class. A sample application is also included to illustrate its use.<\/p>\n<h3><a name=\"1\">System tray (task tray) function<\/a><\/h3>\n<p>The system tray (task tray) is a small icon that lines the lower right corner of the computer screen. Java SE 6 adds the following classes to utilize the system tray functionality.<\/p>\n<ul>\n<li><strong>SystemTray<\/strong><br \/>\n\u3000The SystemTray class represents the desktop system tray.<\/li>\n<li><strong>TrayIcon<\/strong><br \/>\n\u3000The TrayIcon object represents a tray icon that can be added to the system tray.<\/li>\n<\/ul>\n<h3>How to create a blinking icon<\/h3>\n<p>To create a flashing icon, you may use the following site<br \/>Upload a local icon to create a flashing icon.<br \/>\n<a href=\"https:\/\/www.bannerkoubou.com\/anime\/\" target=\"_blank\"><strong>https:\/\/www.bannerkoubou.com\/anime\/<\/strong><\/a><\/p>\n<h2 class=\"common_title\"><a name=\"3\">Sample Applications<\/a><\/h2>\n<p><img src=\"https:\/\/chat-messenger.com\/images\/task_tray.jpg\" alt=\"TaskTray SystemTray java.awt.SystemTray java.awt.TrayIcon\" border=\"0\"><br \/>\n<br clear=\"all\"><\/p>\n<p>\nThe sample application uses the system tray utility class TaskTrayUtil to implement the following functions<\/p>\n<ol>\n<li>When activated.<strong>task tray<\/strong>indicates such things as location of person or thing, location of short-term action, etc.<strong>icon<\/strong>Display.\n<\/li>\n<li>Click the [Flashing Button],<strong>task tray<\/strong>icon blinks.<br \/>\n\tAlso, a balloon with the message \"Get me out of here!!!!!!!!!!!!!!!!!!!!!!!!\" message.<\/li>\n<li>Click [Release button] to cancel the icon blinking.<\/li>\n<li><strong>task tray<\/strong>icon with a single left click to bring the main window out onto the desktop.<\/li>\n<li><strong>task tray<\/strong>icon, right-click to display the default PopupMenu menu.<\/li>\n<li>Once the main window is minimized<strong>task tray<\/strong>Stored in<\/li>\n<\/ol>\n<h2 class=\"common_title\"><a name=\"4\">Source code acquisition<\/a><\/h2>\n<p><script type=\"text\/javascript\" src=\"https:\/\/chat-messenger.com\/js\/common.js?dd12sssas2223\" charset=\"UTF-8\"><\/script><br \/>\n<script type=\"text\/javascript\"> writePR(); <\/script><\/p>\n<p>The source code can be obtained from When compiling, please also save the following icons in the same location as the source coded.<\/p>\n<ul>\n<li><strong>Normal icon<\/strong>\u3000<a href=\"https:\/\/chat-messenger.com\/images\/tray_icon.gif\" target=\"_blank\"><img src=\"https:\/\/chat-messenger.com\/images\/tray_icon.gif\" alt=\"\" border=\"0\"><\/a><\/li>\n<li><strong>Flashing Icons<\/strong>\u3000<a href=\"https:\/\/chat-messenger.com\/images\/tray_icon_light.gif\" target=\"_blank\"><img src=\"https:\/\/chat-messenger.com\/images\/tray_icon_light.gif\" alt=\"\" border=\"0\"><\/a><\/li>\n<\/ul>\n<p><textarea readonly=\"readonly\" style=\"font-size: 13px;height: 400px;\" class=\"src\" onclick=\"this.focus();this.select()\">import java.awt.AWTException;<br \/>\nimport java.awt.Dimension;<br \/>\nimport java.awt.FlowLayout;<br \/>\nimport java.awt.Image;<br \/>\nimport java.awt.MenuItem;<br \/>\nimport java.awt.PopupMenu;<br \/>\nimport java.awt.SystemTray;<br \/>\nimport java.awt.TrayIcon;<br \/>\nimport java.awt.event.ActionEvent;<br \/>\nimport java.awt.event.ActionListener;<br \/>\nimport java.awt.event.MouseAdapter;<br \/>\nimport java.awt.event.MouseEvent;<br \/>\nimport java.awt.event.MouseListener;<br \/>\nimport java.awt.event.WindowAdapter;<br \/>\nimport java.awt.event.WindowEvent;<br \/>\nimport java.awt.event.WindowListener;<\/p>\n<p>import javax.swing.ImageIcon;<br \/>\nimport javax.swing.JButton;<br \/>\nimport javax.swing.JFrame;<br \/>\nimport javax.swing.JPanel;<br \/>\nimport javax.swing.SwingUtilities;<br \/>\nimport javax.swing.UIManager;<\/p>\n<p>\/**<br \/>\n * Display\/blink the task tray (system tray) icon in Java.<br \/>\n *<br \/>\n * \u25c6 Overview of this class<br \/>\n * The TaskTray class is a utility that handles the task tray.<br \/>\n * Provides the following features:<br \/>\n * \u30fbDisplay the icon in the task tray.<br \/>\n * - Display a pop-up message in the task tray.<br \/>\n * - Make the task tray blink.<br \/>\n *<br \/>\n * \u25c6 How to use<br \/>\n * Pass the target JFrame class to the method argument below.<br \/>\n * \u30fbcreateTray(JFrame targetFrame)<br \/>\n * \u30fbcreateTray(final JFrame targetFrame, Image image, PopupMenu menu)<br \/>\n *<br \/>\n *\/<br \/>\npublic class TaskTrayUtil {<br \/>\n    private static JFrame frame;<br \/>\n    private static PopupMenu defaultMenu;<br \/>\n    private static TrayIcon trayIcon;<br \/>\n    private static SystemTray tray = SystemTray.getSystemTray();<br \/>\n    private static Object INSTANCE_LOCK = new Object();<\/p>\n<p>    \/**<br \/>\n     * Execution sample<br \/>\n     * @param args<br \/>\n     *\/<br \/>\n    public static void main(String[] args) {<br \/>\n      try {<br \/>\n          \/\/ Set LookAndFeel to Windows<br \/>\n          UIManager.setLookAndFeel(<br \/>\n                  UIManager.getSystemLookAndFeelClassName());<br \/>\n          \/\/ regular icon<br \/>\n          final Image img = new ImageIcon(<br \/>\n                  TaskTrayUtil.class.getResource(<br \/>\n                          \u201ctray_icon.gif\u201d)).getImage();<br \/>\n          final JFrame frame =<br \/>\n              new JFrame(\u201cDisplay\/blink icon in task tray\u201d);<br \/>\n          frame.setSize(300, 100);<br \/>\n          frame.setIconImage(img);<br \/>\n          \/\/ Store in task tray when window is minimized<br \/>\n          \/\/ Add WindowListener class as shown.<br \/>\n          frame.addWindowListener(new WindowAdapter() {<br \/>\n              public void windowIconified(WindowEvent e) {<br \/>\n                  frame.setVisible(false);<br \/>\n              }<br \/>\n          });<\/p>\n<p>          JPanel jPanel = new JPanel();<br \/>\n          jPanel.setLayout(new FlowLayout());<br \/>\n          jPanel.setSize(new Dimension(219, 70));<br \/>\n          \/\/ blinking icon<br \/>\n          final Image imgLight =<br \/>\n              new ImageIcon(TaskTrayUtil.class.getResource(<br \/>\n                      \u201ctray_icon_light.gif\u201d)).getImage();<br \/>\n          JButton lightButton = new JButton(\u201cblink\u201d);<br \/>\n          lightButton.addActionListener(new ActionListener(){<br \/>\n              public void actionPerformed(ActionEvent e) {<br \/>\n                  onAndOffLight(imgLight, null);<br \/>\n                  displayMessage(\u201cMessage\u201d, \u201cGet me out of here!!\u201d);<br \/>\n              }<br \/>\n          });<br \/>\n          JButton cancelButton = new JButton(\u201ccancel\u201d);<br \/>\n          cancelButton.addActionListener(new ActionListener(){<br \/>\n              public void actionPerformed(ActionEvent e) {<br \/>\n                  replaceImageWithDelete(img);<br \/>\n              }<br \/>\n          });<br \/>\n          jPanel.add(lightButton, null);<br \/>\n          jPanel.add(cancelButton, null);<br \/>\n          frame.add(jPanel);<br \/>\n          \/\/ End processing.<br \/>\n          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<br \/>\n          \/\/ Display the frame in the center of the screen.<br \/>\n          frame.setLocationRelativeTo(null);<br \/>\n          \/\/ Create task tray.<br \/>\n          TaskTrayUtil.createTray(frame);<br \/>\n          frame.setVisible(true);<\/p>\n<p>      } catch (Exception e) {<br \/>\n          e.printStackTrace();<br \/>\n      }<br \/>\n    }<br \/>\n    \/**<br \/>\n     *<br \/>\n     *\/<br \/>\n    static {<br \/>\n        createPopupMenu();<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * Generates a default PopupMenu menu to be displayed in the task tray.<br \/>\n     *The following menus are available.<br \/>\n     * \u30fb&quot;Remove from task tray&quot;<br \/>\n     * Move the target application from the task tray to the desktop<br \/>\n     * Take it out.<br \/>\n     * (Activate the window and bring it to the front.)<br \/>\n     * - Terminate the application to be terminated.<br \/>\n     *\/<br \/>\n    private static void createPopupMenu() {<br \/>\n        MenuItem getTrayItem = new MenuItem(\u201cRemove from task tray\u201d);<br \/>\n        getTrayItem.addActionListener(new ActionListener() {<br \/>\n            public void actionPerformed(ActionEvent e) {<br \/>\n                active();<br \/>\n            }<br \/>\n        });<br \/>\n        MenuItem exitItem = new MenuItem(\u201cExit\u201d);<br \/>\n        exitItem.addActionListener(new ActionListener() {<br \/>\n            public void actionPerformed(ActionEvent e) {<br \/>\n                removeTrayIcon();<br \/>\n                TaskTrayUtil.frame.setVisible(false);<br \/>\n                TaskTrayUtil.frame.dispose();<br \/>\n                System.exit(0);<br \/>\n            }<br \/>\n        });<br \/>\n        defaultMenu = new PopupMenu();<br \/>\n        defaultMenu.add(getTrayItem);<br \/>\n        defaultMenu.add(exitItem);<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * Create a tray icon and add it to the system tray.<br \/>\n     * Set a listener for events that occur on the icon to targetFrame<br \/>\n     Set to *.<br \/>\n     * The event here is when the tray icon is double clicked.<br \/>\n     * Extract the target application.<br \/>\n     * @param targetFrame Target application<br \/>\n     *\/<br \/>\n    public static void createTray(JFrame targetFrame) {<br \/>\n        createTray(targetFrame, null, null);<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * Create a tray icon and add it to the system tray.<br \/>\n     *<br \/>\n     * @param targetFrame Target application<br \/>\n     * @param image Image image to display on the tray icon.<br \/>\n     * If null, it can be obtained from targetFrame with getIconImage()<br \/>\n     * Use image images.<br \/>\n     * @param menu PopupMenu to display in the task tray.<br \/>\n     * If null, display the default PopupMenu.<br \/>\n     *\/<br \/>\n    public static void createTray(final JFrame targetFrame, Image image,<br \/>\n            PopupMenu menu) {<br \/>\n        \/\/ Exit if system tray is not supported.<br \/>\n        if (!SystemTray.isSupported()) {<br \/>\n            return;<br \/>\n        }<br \/>\n        TaskTrayUtil.frame = targetFrame;<br \/>\n        if (image == null) {<br \/>\n            image = targetFrame.getIconImage();<br \/>\n        }<br \/>\n        if (menu == null) {<br \/>\n            menu = defaultMenu;<br \/>\n        }<br \/>\n        trayIcon = new TrayIcon(image, targetFrame.getTitle(), menu);<br \/>\n        trayIcon.setImageAutoSize(true);<br \/>\n        trayIcon.addMouseListener(new MouseAdapter(){<br \/>\n            public void mouseClicked(MouseEvent e) {<br \/>\n                if (SwingUtilities.isLeftMouseButton(e)) {<br \/>\n                    active();<br \/>\n                }<br \/>\n            }<br \/>\n        });<br \/>\n        try {<br \/>\n            tray.add(trayIcon);<br \/>\n        } catch (AWTException e1) {<br \/>\n            e1.printStackTrace();<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * Remove icon from task tray.<br \/>\n     * Must be called when the application exits.<br \/>\n     *\/<br \/>\n    public static void removeTrayIcon() {<br \/>\n        tray.remove(trayIcon);<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * Make the task tray icon blink.<br \/>\n     * @param msg if necessary when the blinking icon is clicked<br \/>\n     * Set the message to display.<br \/>\n     *\/<br \/>\n    public static void onAndOffLight(Image lightImg, String msg) {<br \/>\n        replaceImage(lightImg);<br \/>\n        ChangeLightImgAdapter adap =<br \/>\n            new TaskTrayUtil().new ChangeLightImgAdapter(msg);<br \/>\n        trayIcon.addMouseListener(adap);<br \/>\n        frame.addWindowListener(adap);<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * Change the task tray icon.<br \/>\n     * @param image Change only if it differs from the current icon.<br \/>\n     *\/<br \/>\n    public static void replaceImage(Image image) {<br \/>\n        synchronized (INSTANCE_LOCK) {<br \/>\n            if (!image.equals(trayIcon.getImage())) {<br \/>\n                trayIcon.setImage(image);<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * Change the task tray icon.<br \/>\n     * *If you want to restore the icon that is blinking for some reason,<br \/>\n     * It seems to be useless unless you delete it with {@link SystemTray#remove(TrayIcon)}).<br \/>\n     * @param image Change only if it differs from the current icon.<br \/>\n     *\/<br \/>\n    public static void replaceImageWithDelete(Image image) {<br \/>\n        synchronized (INSTANCE_LOCK) {<br \/>\n            if (!image.equals(trayIcon.getImage())) {<br \/>\n                tray.remove(trayIcon);<br \/>\n                trayIcon.setImage(image);<br \/>\n                try {<br \/>\n                    tray.add(trayIcon);<br \/>\n                } catch (Exception e) {<br \/>\n                    e.printStackTrace();<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * Display a pop-up message in the task tray.<br \/>\n     * @param caption<br \/>\n     * @param text<br \/>\n     *\/<br \/>\n    public static void displayMessage(String caption, String text) {<br \/>\n        if (caption != null || text != null) {<br \/>\n            trayIcon.displayMessage(caption, text,<br \/>\n                    TrayIcon.MessageType.INFO);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * Move the target application from the task tray to the desktop<br \/>\n     * Take it out.<br \/>\n     *\/<br \/>\n    private static void active() {<br \/>\n        \/\/ Return the frame state to normal.<br \/>\n        TaskTrayUtil.frame.setExtendedState(JFrame.NORMAL);<br \/>\n        TaskTrayUtil.frame.setAlwaysOnTop(true);<br \/>\n        TaskTrayUtil.frame.setVisible(true);<br \/>\n        TaskTrayUtil.frame.setAlwaysOnTop(false);<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * Event to be generated when the blinking icon is clicked<br \/>\n     * Listener class.<br \/>\n     *\/<br \/>\n    class ChangeLightImgAdapter implements WindowListener, MouseListener {<br \/>\n        String lightImageText;<\/p>\n<p>        public ChangeLightImgAdapter(String lightImageText) {<br \/>\n            this.lightImageText = lightImageText;<br \/>\n        }<\/p>\n<p>        \/**<br \/>\n         * When the task tray icon is clicked<br \/>\n         * Show popup message.<br \/>\n         *\/<br \/>\n        public void mousePressed(MouseEvent e) {<br \/>\n            TaskTrayUtil.displayMessage(null, lightImageText);<br \/>\n        }<\/p>\n<p>        \/**<br \/>\n         * Blinked when the target application became active<br \/>\n         * Return icon.<br \/>\n         *\/<br \/>\n        public void windowActivated(WindowEvent e) {<br \/>\n            TaskTrayUtil.replaceImageWithDelete(TaskTrayUtil.frame<br \/>\n                    .getIconImage());<br \/>\n            TaskTrayUtil.frame.removeWindowListener(this);<br \/>\n            TaskTrayUtil.trayIcon.removeMouseListener(this);<br \/>\n        }<\/p>\n<p>        public void windowClosed(WindowEvent e) {}<\/p>\n<p>        public void windowClosing(WindowEvent e) {}<\/p>\n<p>        public void windowDeactivated(WindowEvent e) {}<\/p>\n<p>        public void windowDeiconified(WindowEvent e) {}<\/p>\n<p>        public void windowIconified(WindowEvent e) {}<\/p>\n<p>        public void windowOpened(WindowEvent e) {}<\/p>\n<p>        public void mouseClicked(MouseEvent e) {}<\/p>\n<p>        public void mouseEntered(MouseEvent e) {}<\/p>\n<p>        public void mouseExited(MouseEvent e) {}<\/p>\n<p>        public void mouseReleased(MouseEvent e) {}<br \/>\n    }<br \/>\n}<br \/>\n<\/textarea><\/p>","protected":false},"excerpt":{"rendered":"<p>Display\/blink the task tray (system tray) icon in Java [\u2026]<\/p>","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"swell_btn_cv_data":""},"categories":[19],"tags":[],"_links":{"self":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/906"}],"collection":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/comments?post=906"}],"version-history":[{"count":14,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/906\/revisions"}],"predecessor-version":[{"id":3679,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/906\/revisions\/3679"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/media?parent=906"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/categories?post=906"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/tags?post=906"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}