user271865
user271865

Reputation: 25

How to set system-tray icon using only AWT

I've created a calculator using AWT Frames. I wanna know how to add a tray-icon to my Cal. I can only use AWT not Swing.

Upvotes: 2

Views: 790

Answers (1)

Jean-François Savard
Jean-François Savard

Reputation: 21004

final TrayIcon trayIcon = new TrayIcon(Toolkit.getDefaultToolkit().createImage("pathToImage"));
final SystemTray tray = SystemTray.getSystemTray();

try {
    tray.add(trayIcon);
} catch (AWTException e) {
    System.out.println("TrayIcon could not be added.");
}

Found at this page http://docs.oracle.com/javase/tutorial/uiswing/misc/systemtray.html

You should also check if SystemTray is supported before using the following snippet

if (!SystemTray.isSupported()) {
    System.out.println("SystemTray is not supported");
    //..
}

Note that SystemTray is from package java.awt as per your request.

Upvotes: 2

Related Questions