Monday, 27 May 2013

This blog explains about how to create own system tray icon using java.

what is system tray icon ??????



Copy the below code and run as Java Appication . After execution see your system tray.
Don't close the application, otherwise your application icon will also get vanish from that tray.

Java Source Code :
 


import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;

/**
 *
 * @author Rupesh Shirude
 *
 */

public class MyAppTrayIcon implements ActionListener, ItemListener{

  PopupMenu popup = null;
  TrayIcon trayIcon = null;
  SystemTray tray  = null;
  MenuItem aboutItem =  null;
  CheckboxMenuItem showImageOnIcon = null;
  Menu displayMenu = null;
  MenuItem errorItem = null;
  MenuItem warningItem = null;
  MenuItem infoItem = null;
  MenuItem noneItem = null;
  MenuItem exitItem = null;
    
 public MyAppTrayIcon() {
     popup = new PopupMenu();
        trayIcon = new TrayIcon(createImage("images/Koala.jpg", "MyApp"));
        tray = SystemTray.getSystemTray();
        aboutItem = new MenuItem("About MyApp");
        showImageOnIcon = new CheckboxMenuItem("Set auto size");
        displayMenu = new Menu("Display");
        errorItem = new MenuItem("Error");
        warningItem = new MenuItem("Warning");
        infoItem = new MenuItem("Info");
        noneItem = new MenuItem("None");
        exitItem = new MenuItem("Exit");
      
        popup.add(aboutItem);
        popup.addSeparator();
        popup.add(showImageOnIcon);
        popup.addSeparator();
        popup.add(displayMenu);
        displayMenu.add(errorItem);
        displayMenu.add(warningItem);
        displayMenu.add(infoItem);
        displayMenu.add(noneItem);
        popup.add(exitItem);
       
        trayIcon.setPopupMenu(popup);
       
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.out.println("Problem while adding tray icon...");
            return;
        }
       
        trayIcon.addActionListener(this);
        aboutItem.addActionListener(this);
       
        showImageOnIcon.addItemListener(this);
        showImageOnIcon.addItemListener(this);
       
        errorItem.addActionListener(this);
        warningItem.addActionListener(this);
        infoItem.addActionListener(this);
        noneItem.addActionListener(this);
       
        exitItem.addActionListener(this);
 }


    public static void main(String[] args) {
     new MyAppTrayIcon();
    }
   
    protected static Image createImage(String path, String description) {
        URL imageURL = MyAppTrayIcon.class.getResource(path);
       
        if (imageURL == null) {
            System.err.println("Resource not found: " + path);
            return null;
        } else {
            return (new ImageIcon(imageURL, description)).getImage();
        }
    }

 @Override
 public void actionPerformed(ActionEvent event) {
  if(event.getSource() == trayIcon){
   JOptionPane.showMessageDialog(null,
               "This dialog box is run from System Tray");
  }else if(event.getSource() == aboutItem ){
    JOptionPane.showMessageDialog(null,
                 "This dialog box is run from the About menu item");
  }else if(event.getSource() == errorItem){
   trayIcon.displayMessage("Sun TrayIcon Demo",
                "This is an error message", TrayIcon.MessageType.ERROR);
  }else if(event.getSource() == warningItem){
    trayIcon.displayMessage("Sun TrayIcon Demo",
                 "This is a warning message", TrayIcon.MessageType.WARNING);
  }else if(event.getSource() == infoItem){
   trayIcon.displayMessage("Sun TrayIcon Demo",
                "This is an info message", TrayIcon.MessageType.INFO);
  }else if(event.getSource() == noneItem){
   trayIcon.displayMessage("Sun TrayIcon Demo",
                "This is an ordinary message", TrayIcon.MessageType.NONE);
  }else if(event.getSource() == exitItem){
    tray.remove(trayIcon);
             System.exit(0);
  }
 
 }

 @Override
 public void itemStateChanged(ItemEvent event) {
  if(event.getSource() == showImageOnIcon){
    int cb1Id = event.getStateChange();
             if (cb1Id == ItemEvent.SELECTED){
                 trayIcon.setImageAutoSize(true);
             } else {
                 trayIcon.setImageAutoSize(false);
             }
  }
 }
}


No comments:

Post a Comment