View RSS Feed

Java AWT

Accessing the System Tray

Rate this Entry
by , 03-04-2012 at 10:37 AM (395 Views)
System tray could be accessed by the Java. In Java, class java.awt.SystemTray presents the system tray, for desktop. The static method SystemTray.getSystemTray() calls the operating system’s System tray.
SystemTray.isSupported() is used for checking whether it provides support, before its access. It throws java.land.UnsupportedOperationException in case if it doesn’t support or is not checked for support.

  1. Just one SystemTray’s instance is present in Java application.
  2. SystemTray’s own instance couldn’t be created by an application.
  3. Method like getSystemTray() shall be used by application so that to access instance.
  4. One or more than one Traylcons are present in a SystemTray.
  5. Add() method is used to add the Traylcons.



System Tray could be customized or accessed by this way, as it is given in the following code:
final TrayIcon trayIcon;
Java Code: This is the code to explain how to access the system tray
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...");
            System.exit(0);
        }
    };
            
    PopupMenu popup = new PopupMenu();
    MenuItem defaultItem = new MenuItem("Exit");
    defaultItem.addActionListener(exitListener);
    popup.add(defaultItem);

    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 Performed!",
                TrayIcon.MessageType.INFO);
        }
    };
            
    trayIcon.setImageAutoSize(true);
    trayIcon.addActionListener(actionListener);
    trayIcon.addMouseListener(mouseListener);

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

} else {

    //  System Tray is not supported

}

Submit "Accessing the System Tray" to Facebook Submit "Accessing the System Tray" to Digg Submit "Accessing the System Tray" to del.icio.us Submit "Accessing the System Tray" to StumbleUpon Submit "Accessing the System Tray" to Google

Tags: system tray Add / Edit Tags
Categories
Tray , Java AWT

Comments