“How do applications like Skype reside in System Tray? Can we do something like this for our application, so that the task tray shows our application icon too? Can we do it in JAVA?”
These kinds of questions usually arise in our minds while working. This is what this TIP is all about.
FEATURES:
1. This application will run on the System Task Tray. 2. It will add the application logo to System Task Tray. 3. Clicking the application logo will show a a Popup menu with different menu items.
4. This application will run a process that will show popup messages after every 10 secs.
We will be using Eclipse IDE for this application. Below are the steps to develop the application.
1. Create a “Java Project” in eclipse, name it as “SystemTraySample”.
2. Create a package structure inside the “src” folder. It may be something like “com.mindfire.tray”.
3. Create a class inside that package. Name the class as “SystemTrayExample”.
4. Add an image inside the “src” folder. It can be any image that u want as the application logo. Name the image as “logo.jpg”.
5. Now add the below code to the class.
package com.mindfire.tray;
import java.awt.AWTException; import java.awt.CheckboxMenuItem; import java.awt.Image; import java.awt.Menu; import java.awt.MenuItem; import java.awt.PopupMenu; import java.awt.SystemTray; import java.awt.TrayIcon; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
public class SystemTrayExample {
String TOOL_TIP = “Mindfire Tool Tip”; String MESSAGE_HEADER = “Mindfire SystemTray Example”;
TrayIcon processTrayIcon = null;
public static void main(String[] args) { try { SystemTrayExample systemTrayExample = new SystemTrayExample(); systemTrayExample.createAndAddApplicationToSystemTray(); systemTrayExample.startProcess(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
}
/** * This method creates the AWT items and add it to the System tray. * * @throws IOException */ private void createAndAddApplicationToSystemTray() throws IOException { // Check the SystemTray support if (!SystemTray.isSupported()) { System.out.println(“SystemTray is not supported”); return;
}
final PopupMenu popup = new PopupMenu(); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); InputStream inputStream = classLoader.getResourceAsStream(“logo.jpg”);
Image img = ImageIO.read(inputStream);
final TrayIcon trayIcon = new TrayIcon(img, TOOL_TIP); this.processTrayIcon = trayIcon;
final SystemTray tray = SystemTray.getSystemTray();
// Create a popup menu components
MenuItem aboutItem = new MenuItem(“About”);
CheckboxMenuItem autoSizeCheckBox = new CheckboxMenuItem(“Set auto size”);
CheckboxMenuItem toolTipCheckBox = new CheckboxMenuItem(“Set tooltip”);
Menu displayMenu = new Menu(“Display”);
MenuItem errorItem = new MenuItem(“Error”); MenuItem warningItem = new MenuItem(“Warning”); MenuItem infoItem = new MenuItem(“Info”);
MenuItem noneItem = new MenuItem(“None”);
MenuItem exitItem = new MenuItem(“Exit”);
// Add components to popup menu popup.add(aboutItem); popup.addSeparator(); popup.add(autoSizeCheckBox); popup.add(toolTipCheckBox); popup.addSeparator(); popup.add(displayMenu); displayMenu.add(errorItem); displayMenu.add(warningItem); displayMenu.add(infoItem); displayMenu.add(noneItem);
popup.add(exitItem);
trayIcon.setPopupMenu(popup);
// Setting toolTipCheck and autoSizeCheckBox state as true toolTipCheckBox.setState(true); autoSizeCheckBox.setState(true);
trayIcon.setImageAutoSize(true);
try { tray.add(trayIcon); } catch (AWTException e) { System.out.println(“TrayIcon could not be added.”); return;
}
// Add listener to trayIcon. trayIcon.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, “This dialog box is run from System Tray”); }
});
// Add listener to aboutItem. aboutItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, “This dialog box is run from the About menu item”); }
});
// Add listener to autoSizeCheckBox. autoSizeCheckBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { int autoSizeCheckBoxId = e.getStateChange(); if (autoSizeCheckBoxId == ItemEvent.SELECTED) { trayIcon.setImageAutoSize(true); } else { trayIcon.setImageAutoSize(false); } }
});
//Add listener to toolTipCheckBox. toolTipCheckBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { int toolTipCheckBoxId = e.getStateChange(); if (toolTipCheckBoxId == ItemEvent.SELECTED) { trayIcon.setToolTip(TOOL_TIP); } else { trayIcon.setToolTip(null); } }
});
// Create listener for Display menu items. ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { MenuItem item = (MenuItem) e.getSource(); System.out.println(item.getLabel()); if ("Error".equals(item.getLabel())) { trayIcon.displayMessage(MESSAGE_HEADER,"This is an error message",TrayIcon.MessageType.ERROR); } else if ("Warning".equals(item.getLabel())) { trayIcon.displayMessage(MESSAGE_HEADER,"This is a warning message",TrayIcon.MessageType.WARNING); } else if ("Info".equals(item.getLabel())) { trayIcon.displayMessage(MESSAGE_HEADER,"This is an info message",TrayIcon.MessageType.INFO); } else if ("None".equals(item.getLabel())) { trayIcon.displayMessage(MESSAGE_HEADER,"This is an ordinary message",TrayIcon.MessageType.NONE); } }
};
// Add listeners to Display menu items. errorItem.addActionListener(listener); warningItem.addActionListener(listener); infoItem.addActionListener(listener);
noneItem.addActionListener(listener);
// Add listener to exitItem. exitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { tray.remove(trayIcon); System.exit(0); } });
}
/** * This method will start a thread that will * show a popup message from the system tray after every 10 secs.
*/
private void startProcess() {
Thread thread = new Thread(new Runnable() {
@Override public void run() { // TODO Auto-generated method stub int i = 0; while (true) { i++; try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } processTrayIcon.displayMessage(“Mindfire Process Message”, “This is message number ” + i, TrayIcon.MessageType.INFO);
}
} }); thread.start(); } }
6. Now run the application. The application icon should be added in the System Tray. Right – click on the logo will show a popup menu with some menu items. And a popup message will get displayed in every 10 secs.