Page 1 of 1

Add a system tray icon to your swing app. Rate Topic: -----

#1 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 851
  • View blog
  • Posts: 2,598
  • Joined: 29-July 11

Posted 06 September 2011 - 11:30 PM

*
POPULAR

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.

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 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);
        frame.setVisible(true);
    }

    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(e.getMessage());
            }
        }
    }//end FrameTest constructor

    //main
    public static void main(String[]args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new FrameTest();
            }
        });
    }//end main()

}//end FrameTest Class



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.

This post has been edited by farrell2k: 08 September 2011 - 02:42 PM


Is This A Good Question/Topic? 5
  • +

Replies To: Add a system tray icon to your swing app.

#2 uzzi7862  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 28-February 12

Posted 14 March 2012 - 05:04 AM

Hello. Awesome tutorial i have been looking for somthing like this for quite a while now. I was just wondering what could be the cause for my image not displaying on the system tray. Since i have tried a few images and it did not want to work.
Was This Post Helpful? 0
  • +
  • -

#3 tmcraig  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 01-February 13

Posted 04 February 2013 - 02:49 AM

Good tutorial,

But there seems to be an issue when it comes to windows 7. It creates a blank icon in the sys-tray.

Anyone have a fix for this?

can we not edit posts??

forgot to add, tried lots of other varieties of this tutorial, and all seem to suffer the same problem. most people mention to use the autosize function. But this only works on XP when people are using wrong sized icons.

Craig

can we not edit posts??

forgot to add, tried lots of other varieties of this tutorial, and all seem to suffer the same problem. most people mention to use the autosize function. But this only works on XP when people are using wrong sized icons.

Craig
Was This Post Helpful? 0
  • +
  • -

#4 Brainpower49  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 04-February 13

Posted 04 February 2013 - 09:05 PM

Nicely done!
The tutorial is easy to follow. Plus, I'm not to familiar with java AWT and GUIS.
Everything was understandable thanks to clear writing and the comments in code (helped much :D).
I've actually wanted to learn how to do this before. But my search skills weren't that good.
Taught me a nice amount, Thanks!
:bananaman:


+1
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1