Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Flash a window to grab user's attention

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...)

like image 427
jjnguy Avatar asked Sep 21 '26 00:09

jjnguy


1 Answers

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
like image 163
John Millikin Avatar answered Sep 22 '26 13:09

John Millikin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!