Dynamicaly update tooltip to show timer

1.2k views Asked by At

I have an app, that goes to system tray when it's minimized. And i need to show a tooltip with timer on it. Everything is ok, but i can't make tooltip refresh every when tooltip's shown. Can i refresh it somehow else, except making fake mouse event and handleing it to trick tooltip, like here, or it's the only way?

upd:

Code i'm using to set tooltip.

TrayIcon trayIcon;
SystemTray tray;
tray=SystemTray.getSystemTray();
trayIcon=new TrayIcon(image, "tooltipText");
tray.add(trayIcon);

and this one's updating the tooltip

MyClass.trayIcon.setToolTip();
1

There are 1 answers

0
Piro On

TrayIcon is bit problematic because it is not a Component as rest of Java AWT / Swing components. I have found workaround, but it is not exactly same as tooltip. TrayIcon has displayMessage method that can be used instead of tooltip. I have tried to make it behave like tooltip (show on hover) but not all mouse events are supported, so message has to be shown always, or on click on icon. Also i have some concerns about implementation of message showing on other OSes. I have tested it only on MS Windows XP SP3, here is result (it is updating every 1s): screenshot of TrayIcon message

Here is runnable example (you need image.jpeg to run) :

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.TrayIcon.MessageType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.Timer;


public class TestTray {
    final ActionListener menuItemCloseAction = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    };
    final MenuItem menuItemClose = new MenuItem("Close");
    final PopupMenu menu = new PopupMenu();
    final Image image;
    {
        Image tmp = null; 
        try {
            tmp = ImageIO.read(TestTray.class.getResourceAsStream("image.jpeg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        image = tmp;
    }
    final TrayIcon trayIcon = new TrayIcon(image,null,menu);
    final SystemTray tray = SystemTray.getSystemTray();
    final DateFormat dateFormat = new SimpleDateFormat("HH:MM:ss");
    final ActionListener timerAction = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String msg = "Time is: "+ dateFormat.format(new Date());
            //trayIcon.setToolTip(msg);
            trayIcon.displayMessage("", msg, MessageType.NONE);
        }
    };

    final Timer timer = new Timer(100, timerAction);
    {
        menu.add(menuItemClose);
        menuItemClose.addActionListener(menuItemCloseAction);
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            e.printStackTrace();
        }
        timer.start();
    }

    public static void main(String[] args) {
        new TestTray();
    }
}