how-to

Others

How to add icon to system tray

For this example you need:

import java.awt.*;

This is base example that shows how to add icon (from project resources) with caption to system tray. The system must supports it.

/* -- is supported by System ? */
if ( ! SystemTray.isSupported() ) return;

/* -- get system tray */

SystemTray tray = SystemTray.getSystemTray();
System.out.println( tray.getTrayIconSize().toString() );

/* -- load image */

Image image = Toolkit.getDefaultToolkit().getImage( this.getClass().getResource( "/resource/pict2.png" ) );

/* -- create tray icon */

TrayIcon trayicon = new TrayIcon( image, "This is tray icon.." );
trayicon.setImageAutoSize( true );

/* -- add tray icon to system tray */

try {
  tray.add( trayicon );
} catch (AWTException ex) {
  Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, ex);
}

Till end of the appplication you will have this icon in the system tray: