private void initializeSystemTray() {
if (SystemTray.isSupported()) {
URL imageUrl = MainWindow.class.getResource("jdcp-32.png");
Image image = Toolkit.getDefaultToolkit().getImage(imageUrl);
final TrayIcon icon = new TrayIcon(image, "JDCP Worker");
icon.setImageAutoSize(true);
PopupMenu popup = new PopupMenu();
MenuItem popupItem = new MenuItem("Open JDCP Worker");
popupItem.setShortcut(new MenuShortcut(KeyEvent.VK_O));
popupItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MainWindow.this.setVisible(true);
MainWindow.this.setState(JFrame.NORMAL);
MainWindow.this.toFront();
}
});
popup.add(popupItem);
popupItem = new MenuItem("Exit");
popupItem.setShortcut(new MenuShortcut(KeyEvent.VK_X));
popupItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
exit();
}
});
popup.add(popupItem);
icon.setPopupMenu(popup);
icon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MainWindow.this.setVisible(true);
MainWindow.this.setState(JFrame.NORMAL);
MainWindow.this.toFront();
}
});
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
addWindowListener(new WindowAdapter() {
private boolean first = true;
public void windowClosing(WindowEvent e) {
MainWindow.this.setVisible(false);
if (first) {
icon.displayMessage(
"JDCP Worker",
"JDCP Worker is still running. To exit, right click this icon and click 'exit'.",
MessageType.INFO);
first = false;
}