"I heard about some coole new Java 6 Systemtray features. Whats that all about?"
Solution:
Using a TrayIcon you can add your Application to the SystemTray. A TrayIcon always needs an Icon, a Name and a PopupMenu.
Have a look at this Demo-Application:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SystemtrayTest implements Runnable {
public static void main(String[] args) {
EventQueue.invokeLater(new SystemtrayTest());
}
public void run() {
// first check, if the current OS supports SystemTray
if (SystemTray.isSupported()) {
final PopupMenu menu = new PopupMenu();
final MenuItem helloMenu = new MenuItem("Hello World");
final MenuItem exitMenu = new MenuItem("Exit");
helloMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Hello World !");
}
});
exitMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menu.add(helloMenu);
menu.addSeparator();
menu.add(exitMenu);
// Attention! Replace this "arrow_down.gif" with your icon or the TrayIcon will be empty
final Image icon = Toolkit.getDefaultToolkit().getImage("arrow_down.gif");
final TrayIcon trayIcon = new TrayIcon(icon, "SystemTrayTest", menu);
try {
// add the trayIcon to the system tray
SystemTray.getSystemTray().add(trayIcon);
// show an information message in a bubble
trayIcon.displayMessage("Hello", "This is just an information", TrayIcon.MessageType.INFO);
} catch (AWTException ex) {
ex.printStackTrace();
}
} else {
System.err.println("Tray not supported");
}
}
}
Keine Kommentare:
Kommentar veröffentlichen