Search code examples
javaswtsystem-traytrayicontray

Display numbers on a tray icon with SWT


I would like to show some numbers on my tray icon indicating a number of events that happened to the user like what is done in this facebook notifications icons:

enter image description here

Do you think that it is possible ?

Thank you


Solution

  • You can do this using the TaskBar and TaskItem classes although it may not work on all platforms.

    TaskBar taskBar = Display.getDefault().getSystemTaskBar();
    // TODO may return null if not supported on the platform
    
    // Get application item
    
    TaskItem taskItem = taskBar.getItem(null);
    if (taskItem != null)
      taskItem.setOverlayText("your text");
    

    Also try:

    TaskItem taskItem = taskBar.getItem(shell);
    

    where shell is your main application shell. The TaskItem JavaDoc suggests trying both methods of getting the TaskItem:

    For better cross platform support, the application code should first try to set this feature on the TaskItem for the main shell then on the TaskItem for the application.