This is a tutorial on how to add tray icon with a popup menu to your swing applications. It's a lot easier than it sounds.
Tested on XP.
You need to find an image for the tray icon. I just searched dog on google image search and found one. When you have your image, resize it to 16x16 and save it as a gif or jpeg in your NetBeans project directory.
Now you need to create a few objects that represent the system tray, the system tray icon, its popup menu, and the items in the popup menu.
Create an Image object to represent the tray image: Image dogImage = Toolkit.getDefaultToolkit().getImage(./dog.jpg) If you use netbeans place your dog.jpg image in your projects root dir. On my windows box this is My Documents/NetBeansProjects/FrameTest.
Now you need to create a SystemTray object: SystemTray sysTray = SystemTray.getSystemTray();
Your next step is to create the popup menu: Popupmenu menu = new PopupMenu();
Next you need to create a MenuItem object which will be added to the popup menu: MenuItem item1 = new MenuItem("Exit");
Now that you have your menu item, you need to add it to the popup menu with PopUpMenu's .add() method: menu.add(item1);
Next, you add an ActionListener to the menu item so that it does something when you click on it.
You now create your Tray icon object: TrayIcon tray = new TrayIcon(dogImage, "App", menu); SystemTray takes 3 arguments, the Image for the tray, a String for the tooltip when you hover your cursor over the tray icon, and the PopUp Menu.
Finally, you take the TrayIcon you created and add it to the SystemTray.
This may sound complicated, but it was pretty easy to do. Here's my working code.
public class FrameTest {
private JFrame frame;
private Image dogImage;
private SystemTray sysTray;
private PopupMenu menu;
private MenuItem item1;
private TrayIcon trayIcon;
//constructor
public FrameTest() {
initComponents();
//basic stuff.
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initComponents() {
//create jframe
frame = new JFrame("Frame Test");
//check to see if system tray is supported on OS.
if (SystemTray.isSupported()) {
sysTray = SystemTray.getSystemTray();
//create dog image
dogImage = Toolkit.getDefaultToolkit().getImage("./dog.jpg");
//create popupmenu
menu = new PopupMenu();
//create item
item1 = new MenuItem("Exit");
//add item to menu
menu.add(item1);
//add action listener to the item in the popup menu
item1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//create system tray icon.
trayIcon = new TrayIcon(dogImage, "Dog App.", menu);
//add the tray icon to the system tray.
try {
sysTray.add(trayIcon);
}
catch(AWTException e) {
System.out.println("System Tray unsupported!");
}
}
}//end FrameTest constructor
//main
public static void main(String[]args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new FrameTest().setVisible(true);
}
});
}//end main()
}//end FrameTest Class
That should do it. If you've done it correctly, you should see a snazzy little icon in your system tray that responds to right clicks.
Edited by farrell2k, 06 May 2012 - 05:38 PM.
fixed comment.


Sign In
Create Account


Back to top









