Mostrando entradas con la etiqueta barra de tareas. Mostrar todas las entradas
Mostrando entradas con la etiqueta barra de tareas. Mostrar todas las entradas

APLICACION EN SYSTEM TRAY CON JAVA

Posted: martes, 11 de mayo de 2010 by Skuarch in Etiquetas: , ,
0



la siguiente aplicacion es un ejemplo de SystemTray con java, esta funcionalidad fue incorporada en java SE 6

Este es un ejemplo basado en un codigo de oracle

import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class SystemTrayTest {

    private TrayIcon trayIcon = null;

    public SystemTrayTest() throws Exception {

        if (SystemTray.isSupported()) {

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

            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, "Sistema de alarmas ConQuest", popup);

            ActionListener actionListener = new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    trayIcon.displayMessage("Alarma",
                            "una alarma se a presentado",
                            TrayIcon.MessageType.WARNING);
                }
            };

            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 (AWTException e) {
            //        System.err.println("TrayIcon could not be added.");
            //    }

        } else {
            System.err.println("System tray is currently not supported.");
        }
    }


    public void alertWarining(String title, String message) {
        trayIcon.displayMessage(title, message, TrayIcon.MessageType.WARNING);
    }


    public void alertError(String title, String message) {
        trayIcon.displayMessage(title, message, TrayIcon.MessageType.ERROR);
    }

    public void alertInfo(String title, String message) {
        trayIcon.displayMessage(title, message, TrayIcon.MessageType.INFO);
    }

    public void alertNone(String title, String message) {
        trayIcon.displayMessage(title, message, TrayIcon.MessageType.NONE);
    }
} //end class


la clase SystemTrayTest crea una instancia de TrayIcon y verifica si tiene soporte el sistema operativo. implementa los metodos necesarios y crea un menu para mostrar la salida del programa.

la clase SystemTrayTest contiene los metodos alertWarning(), alertError(), alertNone() y alertInfo() para mostrar mensajes en la pantalla


La clase SystemTrayIcon contiene el metodo principal que crea un objeto de SystemTrayTest, utilizando swingWorker

/**
*
* @author skuarch
*/
public class SystemTrayIcon implements Runnable {

    java.awt.TrayIcon trayIcon;
    java.awt.SystemTray tray;

    /**
     * Construct.
     */
    public SystemTrayIcon() {
        super();
    } //end SystemTrayIcon

    /**
     * @param args the command line arguments
     */
    public static void main(String... args) {
        Thread t = new Thread(new SystemTrayIcon());
        t.start();
    } //end main

    @Override
    public void run() {

        try {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    try {
                        SystemTrayTest stt = new SystemTrayTest();
                        stt.alertWarining("Inicio", "este es un mensaje de inicio");
                    } catch (Exception ex) {
                        System.out.println(ex);
                    }
                }
            });
        } catch (Exception e) {
            System.out.println(e);
            throw new UnsupportedOperationException("Not supported yet.");
        }

    } //end run
} //end class


SystemTrayIcon implementa Runnable y dentro del metodo abstracto se crea el siwngWorker.