how-to

Others

How to add icon to system tray and show info message after doubleclick on it ?

For this example you need:

import java.awt.*;

For checking doubleclick on tray icon use and add java.awt.event.ActionListener.

/* -- 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 action listener */

ActionListener actionListener = new ActionListener() {
  @Override
  public void actionPerformed( ActionEvent e ) {
    ( (TrayIcon) e.getSource() ).displayMessage( "Action", "This is action on icon !", TrayIcon.MessageType.INFO );
  }
};

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

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

After doubleclick on the icon is displayed this message: