<p>Is there a better way to flash a window in Java than this:</p>

<pre><code>public static void flashWindow(JFrame frame) throws InterruptedException {
        int sleepTime = 50;
        frame.setVisible(false);
        Thread.sleep(sleepTime);
        frame.setVisible(true);
        Thread.sleep(sleepTime);
        frame.setVisible(false);
        Thread.sleep(sleepTime);
        frame.setVisible(true);
        Thread.sleep(sleepTime);
        frame.setVisible(false);
        Thread.sleep(sleepTime);
        frame.setVisible(true);
}
</code></pre>

<p>I know that this code is scary...But it works alright. (I should implement a loop...)</p>

[{'answer_id': 45234, 'author': 'David Crow', 'author_id': 2783, 'author_profile': 'https://Stackoverflow.com/users/2783', 'pm_score': 1, 'selected': False, 'text': "<p>Well, there are a few minor improvements we could make. ;)</p>\n\n<p>I would use a Timer to make sure callers don't have to wait for the method to return. And preventing more than one flashing operation at a time on a given window would be nice too.</p>\n\n<pre><code>import java.util.Map;\nimport java.util.Timer;\nimport java.util.TimerTask;\nimport java.util.concurrent.ConcurrentHashMap;\nimport javax.swing.JFrame;\n\npublic class WindowFlasher {\n\n    private final Timer timer = new Timer();\n    private final Map&lt;JFrame, TimerTask&gt; flashing\n                              = new ConcurrentHashMap&lt;JFrame, TimerTask&gt;();\n\n    public void flashWindow(final JFrame window,\n                            final long period,\n                            final int blinks) {\n        TimerTask newTask = new TimerTask() {\n            private int remaining = blinks * 2;\n\n            @Override\n            public void run() {\n                if (remaining-- &gt; 0)\n                    window.setVisible(!window.isVisible());\n                else {\n                    window.setVisible(true);\n                    cancel();\n                }\n            }\n\n            @Override\n            public boolean cancel() {\n                flashing.remove(this);\n                return super.cancel();\n            }\n        };\n        TimerTask oldTask = flashing.put(window, newTask);\n\n        // if the window is already flashing, cancel the old task\n        if (oldTask != null)\n            oldTask.cancel();\n        timer.schedule(newTask, 0, period);\n    }\n}\n</code></pre>\n"}, {'answer_id': 45248, 'author': 'John Millikin', 'author_id': 3560, 'author_profile': 'https://Stackoverflow.com/users/3560', 'pm_score': 4, 'selected': True, 'text': '<p>There are two common ways to do this: use JNI to set urgency hints on the taskbar\'s window, and create a notification icon/message. I prefer the second way, since it\'s cross-platform and less annoying.</p>\n\n<p>See <a href="http://java.sun.com/javase/6/docs/api/java/awt/TrayIcon.html" rel="nofollow noreferrer">documentation on the <code>TrayIcon</code> class</a>, particularly the <a href="http://java.sun.com/javase/6/docs/api/java/awt/TrayIcon.html#displayMessage(java.lang.String,%20java.lang.String,%20java.awt.TrayIcon.MessageType)" rel="nofollow noreferrer"><code>displayMessage()</code></a> method.</p>\n\n<p>The following links may be of interest:</p>\n\n<ul>\n<li><a href="http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/systemtray/" rel="nofollow noreferrer">New System Tray Functionality in Java SE 6</a></li>\n<li><a href="http://forums.sun.com/thread.jspa?threadID=5130669&amp;messageID=9469354" rel="nofollow noreferrer">Java Programming - Iconified window blinking</a></li>\n<li><a href="https://jdic.dev.java.net/documentation/incubator/tray/" rel="nofollow noreferrer"><code>TrayIcon</code> for earlier versions of Java</a></li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/45075', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2598/']