HOW TO: Make a System Tray Icon in Java in 3 Steps

to Create System Tray we need 3 things:

1)PopupMenu

2) Menu Items

3)  TrayIcon

Step # 1 :-

so, i will start with PopupMenu

PopupMenu popMenu= new PopupMenu(); 


Step # 2 :-

now we go to step #2 creating menu items, every menu have menu items that you click on it


MenuItem item1 = new MenuItem("Exit");  //here i created menu item named item1 and the text will appear in this menu item is "Exit"


i will make only one item to make it simple, now we created MenuItem, it is time to add it to the Menu itself

popMenu.add(item1);  //adding menu item to popMenu

Note: you can add ActionListener to item1 to execture an action when clicked

item1.addActionListener(this); //adding action listener to item1 

Step # 3 :-

creating TrayIcon object, for tray Icon Object we need 3 Parameters

1)Image to be the Icon

2) String to appear when you put mouse on it.

3) Menu to Appear when you right click on it.

we have the menu and we will pass the String during Object Creation, so we need image to be our Application Icon, so we will create Image Object and Pass the Image Location through Toolkit class

Image img =Toolkit.getDefaultToolkit().getImage("C:/java/cup.jpg");

then we create our TrayIcon Object

TrayIcon trayicon = new TrayIcon(img, "Application Name", popMenu);

now every Applications have Only One System tray, so it is time to add System Tray we created to current System.

SystemTray.getSystemTray().add(trayIcon); //you will need to throw AWTException if the Java Runtime cannot add  the icon to SystemTray

Here is the Full Source Code for System Tray:

 package systemtray;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;

public class Main {

 public static void main(String[] args) throws AWTException {
 PopupMenu popMenu= new PopupMenu();
 MenuItem item1 = new MenuItem("Exit");
 popMenu.add(item1);
 Image img = Toolkit.getDefaultToolkit().getImage("C:/java/cup.jpg");
 TrayIcon trayIcon = new TrayIcon(img, "Application Name", popMenu);
 SystemTray.getSystemTray().add(trayIcon);
 }
}
Advertisements
    • Andrey
    • November 14th, 2010

    Thanks!

      • kebomix
      • November 15th, 2010

      you are welcome, hope it helped 🙂

    • sanda
    • April 8th, 2012

    Thanks bro…..

  1. Its such as you read my thoughts! You seem to grasp a lot about this, such as you wrote
    the book in it or something. I believe that you just can do with some p.

    c. to power the message house a bit, however other than that, this is magnificent blog.
    A great read. I’ll certainly be back.

  2. For most recent information you have to pay a quick visit world-wide-web and on the web I found this site as a most excellent website for most up-to-date updates.

  3. Keep an eye on your blog stats so that you can determine
    great and bad your overall approach. What third-party blogging software does is give you a bit more control, it may give you the opportunity to hold posts, to organize
    them the way you would like, or to embed information that would otherwise be
    difficult to place into your posts. Simply speaking
    blogging means writing and publishing your thoughts and views.

  4. Wow! At last I got a weblog from where I be capable
    of actually get helpful data regarding my study and knowledge.

  5. I used to be suggested this blog by my cousin.
    I am no longer sure whether this submit is written by way of him as nobody else know
    such certain about my problem. You are wonderful! Thanks!

  6. I’ve been exploring for a bit for any high quality articles or weblog
    posts on this kind of area . Exploring in Yahoo I finally stumbled upon this website.

    Studying this info So i’m satisfied to show that I’ve an incredibly good uncanny feeling I
    found out just what I needed. I most no doubt will make certain to don?t fail to remember this site and
    give it a glance on a constant basis.

  7. It’s going to be finish of mine day, except before end I am reading this great piece of writing to increase my experience.

  8. If some one desires expert view on the topic of blogging and site-building then i recommend him/her to pay a quick visit this weblog, Keep up the nice job.

  9. I’ll immediately snatgch your rrss feed as I can’t in finding your emaikl subscription link or e-newsletter service.
    Do you’ve any? Please let me know sso that I may just subscribe.
    Thanks.

  10. Hell friends, how is the whole thing, and wht you wish for to
    say regarding this article, in my view its really amazing in support of me.

  11. Java making a website different lot of url create generate my past experience is not good about Java website develop

  1. November 19th, 2010
  2. September 24th, 2011
  3. January 27th, 2014
    Trackback from : Charles Ayoub News Portal

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s