Java: Flash a window to grab user's attention

java, user-interface

Solution

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.

See documentation on the `TrayIcon` class, particularly the `displayMessage()` method.

The following links may be of interest:

- New System Tray Functionality in Java SE 6

- Java Programming - Iconified window blinking

- `TrayIcon` for earlier versions of Java

Problem

Is there a better way to flash a window in Java than this: ``` 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); } ``` I know that this code is scary...But it works alright. (I should implement a loop...)

Original source