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…)
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
TrayIconclass, particularly thedisplayMessage()method.The following links may be of interest:
TrayIconfor earlier versions of Java