3

I am using java.awt.SystemTray to create and manage the tray icon and balloon messages. Everything works fine.

But I would like to know whether it is possible to close or fade the message after it is displayed.

Right now, the user needs to click the balloon or close it, otherwise the message will not go away.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Saneesh A T
  • 365
  • 1
  • 6
  • 12

1 Answers1

2

I'm not sure about other platforms, but on Windows, the message will only disappear if the machine is in use - i.e. if the user is typing something or moving the mouse. If you don't move the mouse or type anything, the message will stay where it is.

The following code shows a message for me. If I move the mouse, it disappears about 5 seconds later. If I don't, it stays around until such time as I move the mouse or type something, at which point it disappears.

final TrayIcon ti = new TrayIcon(XTPSkin.getInstance().getAppIcon().getImage());
final SystemTray st = SystemTray.getSystemTray();
st.add(ti);
ti.displayMessage("foo", "bar", MessageType.INFO);

There's no direct way to remove the message before its time is up - however, you can remove the TrayIcon (and if necessary immediately re-add it, although this really isn't recommended). Removing the TrayIcon causes the message to also be removed.

The following code, added to the above, causes the TrayIcon and the message to both be removed:

SwingUtilities.invokeLater(new Runnable(){
    public void run() {
        try {
            Thread.sleep(1000); // Don't do this
            st.remove(ti);
        } catch (InterruptedException ie) {
            // You won't need this when the sleep is removed
        }
    }
});

Notes:

  • You need to wrap the first block of code above with some exception handling for AWTException
  • The Thread.sleep in the second block is strictly for demonstration purposes. Since this code will execute on the AWT thread, you should not include that sleep in actual code.
  • Continually removing and re-adding the TrayIcon in this way is probably a bad idea. Since the message does disappear if the user is using the machine, it's probably wisest not to do this.
Jon Bright
  • 13,388
  • 3
  • 31
  • 46
  • Thank you *Bright*. Thank you for your detailed explanation... Then, is there any other method to show some beautiful menus, menu with icon, transparent menu...?? – Saneesh A T Sep 24 '11 at 07:05
  • @SaneeshAT, why would you need one? The method above will get rid of the balloon if you really need to. You can then immediately re-add the tray icon. Either way, that's a separate question. You should ask a new question if you need an answer to that. If you like my answer to your original question, you can click on the check mark next to it. – Jon Bright Sep 24 '11 at 17:10
  • Maybe you could also arbitrarily move the mouse slightly, which will cause it to fade away eventually? – rogerdpack Dec 19 '12 at 06:36
  • Another option may be to use jna/jni/jnr or what not, enumerate the active desktop windows, discover the one that is your balloon message, and send it a close message of some sort, I guess :) – rogerdpack Jan 11 '13 at 03:26
  • removing the tray icon and re-adding it will solve the problem, thanks ! – Amr Lotfy May 08 '14 at 16:00