Monday, September 6, 2010

Hot to make a system Tray icon using Java

To load objects or Images in the system tray using Java copy and paste this code to begin your work.

package systemtray;

import java.awt.*;
import java.awt.event.*;

public class SystemTrayTest
{
  
    public SystemTrayTest()throws AWTException
    {
      
        final TrayIcon trayIcon;

        if (SystemTray.isSupported()) {

            SystemTray tray = SystemTray.getSystemTray();
            Image image = Toolkit.getDefaultToolkit().getImage("tray.gif");

            MouseListener mouseListener = new MouseListener() {
              
                public void mouseClicked(MouseEvent e) {
                    System.out.println("Tray Icon - Mouse clicked!");                
                }
                public void mouseEntered(MouseEvent e) {
                    System.out.println("Tray Icon - Mouse entered!");                
                }
                public void mouseExited(MouseEvent e) {
                    System.out.println("Tray Icon - Mouse exited!");                
                }
                public void mousePressed(MouseEvent e) {
                    System.out.println("Tray Icon - Mouse pressed!");                
                }
                public void mouseReleased(MouseEvent e) {
                    System.out.println("Tray Icon - Mouse released!");                
                }

            };

            ActionListener exitListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Exiting...");
                  //I've added my code to Exit Add Your code here to perform various actions.
                    System.exit(0);
                }
            };
            //creates a pop up when we make a right click on the system tray object
            PopupMenu popup = new PopupMenu();
            MenuItem defaultItem = new MenuItem("Exit");
            defaultItem.addActionListener(exitListener);
            popup.add(defaultItem);
            //creates a pop up when we make a right click on the system tray object
   MenuItem defaultItem1 = new MenuItem("please dont exit");
            defaultItem.addActionListener(exitListener);
            popup.add(defaultItem1);
            //Creates a tray icon with the image u provide
            trayIcon = new TrayIcon(image, "Tray Demo", popup);

            ActionListener actionListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    trayIcon.displayMessage("Action Event",
                        "An Action Event Has Been Peformed!",
                        TrayIcon.MessageType.INFO);
                }
            };
          
            trayIcon.setImageAutoSize(true);
            trayIcon.addActionListener(actionListener);
            trayIcon.addMouseListener(mouseListener);

            //    Depending on which Mustang build you have, you may need to uncomment
            //    out the following code to check for an AWTException when you add
            //    an image to the system tray.

            //    try {
                      tray.add(trayIcon);
            //    } catch (Exception e) {
            //        System.err.println("TrayIcon could not be added.");
            //    }

        } else {
            System.err.println("System tray is currently not supported.");
        }
    }
  
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)throws AWTException
    {
        SystemTrayTest main = new SystemTrayTest();
    }
  
}
Dont forget to add the image tray.gif in the classpath

2 comments:

  1. I'm 15 years old. I was born with HIV my mother passed away because of the HIV infection And I regret why i never met Dr Itua he could have cured my mum for me because as a single mother it was very hard for my mother I came across Dr itua healing words online about how he cure different disease in different races diseases like HIV/Aids Herpes Copd Diabetes Hepatitis even Cancer I was so excited but frighten at same time because I haven't come across such thing article online then I contacted Dr Itua on Mail drituaherbalcenter@gmail.com I also chat with him on what's app +2348149277967 he tells me how it works then I tell him I want to proceed I paid him so swiftly Colorado post office I receive my herbal medicine within 4/5 working days he gave me guild lines to follow and here am I living healthy again can imagine how god use men to manifest his works am I writing in all articles online to spread the god work of Dr Itua Herbal Medicine Yes can ask me anything about this on my twitter @ericamilli or text 7205992850.He's a Great Man.

    ReplyDelete