Jump to content

Adding a System Tray Icon with Popup Menu to your Swing Application.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
farrell2k

farrell2k

    Learning Programmer

  • Members
  • PipPipPip
  • 60 posts
System tray icons with popup menus - All the cool kids' apps have them, and so can yours.

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.


#2
farrell2k

farrell2k

    Learning Programmer

  • Members
  • PipPipPip
  • 60 posts
EDIT: One feature I like in applications that take advantage of the system tray is the ability to minimize an application to the system tray. My favorite Bittorrent app, vuze, allows you to do this. As vuze is written in Java, I started thinking about how easy it would be to implement this, and surprisingly, it is very easy.

Here's how to modify the code in my system tray example (above) to minimize your app to the system tray so that it takes up no space on your task bar:

The first thing you need to do is modify the JFrame's default close operation. Search the constructor for:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
and set it to:

frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
This makes it so that instead of exiting when you click 'x' in the title bar, the applications hides itself from view and takes up no space on your task bar. Great. First part done, but now how do we bring it back? It's surprisingly simple. All we do is set the frame to visible. :)

We need to add another item to the popup menu we created in my first post. We add this to the top of the class, under MenuItem item1:

private MenuItem item2;
Now, in the initComponents() method we add:

item2 = new MenuItem("Show app");

//add actionListener to second menu item
item2.addActionListener(new ActionListerner() {
    public void actionPerformed(ActionEvent e) {
        frame.setVisible(true);
    }
});

//add second item to popup menu
menu.add(item2);
That's it! When you close the app by click x at the top of the app, it should hide and minimize to the system tray. Right clicking on the tray icon should allow you to then "show app" to bring it back.

#3
handita

handita

    Newbie

  • Members
  • Pip
  • 7 posts
nice share. let's try..

#4
NomNom

NomNom

    Newbie

  • Members
  • Pip
  • 8 posts
Pretty cool. Learned something new today! Thanks! +rep
-NomNom