trayIcon displayMessage does not appear in the taskbar java

0

My problem is that I do not get the message in my taskbar using a displayMessage

I have the following code:

private JFrame parent;
    private PopupMenu popup = new PopupMenu();
    private final Image image = new ImageIcon(getClass().getResource("Notificacion.png")).getImage();
    private final TrayIcon trayIcon = new TrayIcon(image, "Notificacion.png", popup);
    //para el Timer
    private Timer timer;
    ///////////////////////////
    SystemTray systemTray;

Class constructor

public Notificacion(JFrame frame) {
        this.parent = frame;
        instanciarTray();
        segundoPlano();
    }

The methods instantiateTray () and secondPlan ()

private void instanciarTray() {
        trayIcon.setImageAutoSize(true);
        systemTray = SystemTray.getSystemTray();
    }

    private void segundoPlano() {
        try {
            if (SystemTray.isSupported()) {
                systemTray.add(trayIcon);
                parent.setVisible(false);

                //Se inicia una tarea cuando se minimiza           
                if (timer != null) {
                    timer.cancel();
                }
                timer = new Timer();
                timer.schedule(new miTimerTask(), 2000, 5000);//Se ejecuta cada 5 segundos
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR);
        }
    }

I have a method called MessageTrayIcon which I use to call it from my class miTimerTask which runs every X seconds but it does not work, I do not get the notification message .

public void MensajeTrayIcon(String texto, MessageType tipo) {
        trayIcon.displayMessage("Advertencia.", texto, tipo);
    }

Class miTimerTask

public class miTimerTask extends TimerTask{

    Controlador controlador = new Controlador();
    Notificacion notificacion = new Notificacion();

    @Override
    public void run() {
        buscarAchivosExtenciones();
    }

    public void buscarAchivosExtenciones(){
        controlador.buscarArchivosPndUnicos(new File("D:\Gerardo\Sustemas\Java\Netbans\Alerta\Alerta\src\CarpetaDirectorio\Nueva carpeta"));
        notificacion.MensajeTrayIcon("Haciendo algo: ", MessageType.INFO);
    }
}

If I call my message method within secondPlan I get the message in the notifications bar but only once, I need it to leave every time the system performs an action, that's why I call it in my class miTimerTask but the call does not work, I'm not doing it wrong. Thanks again.

    
asked by Gerardo Ferreyra 13.09.2017 в 04:04
source

1 answer

0

I was able to solve my problem in the following way. The basis of my problem is that I was doing a class apart from my class Notification , the message did not come out because the class myTimerTask was not included in the class Notification which should put myTimerTask in as an internal class, so that the message gets out, leaving my class Notification

public class Notificacion {

    ArrayList<String> keyArray = new ArrayList<>();
//    LeerArchivos leer = new LeerArchivos();
    private JFrame parent;
    private PopupMenu popup = new PopupMenu();
    private final Image image = new ImageIcon(getClass().getResource("notificaciones.png")).getImage();
    private final TrayIcon trayIcon = new TrayIcon(image, "Notificacion.png", popup);
    //para el Timer
    private Timer timer;
    ///////////////////////////
    SystemTray systemTray;

    public Notificacion() {
    }

    public void instanciarTray() {
        trayIcon.setImageAutoSize(true);
        systemTray = SystemTray.getSystemTray();
    }

    public void segundoPlano() {
        try {
            if (SystemTray.isSupported()) {
                systemTray.add(trayIcon);

                //Se inicia una tarea cuando se minimiza           
                if (timer != null) {
                    timer.cancel();
                }
                timer = new Timer();
                timer.schedule(new miTimerTask(), 1000, segundos());//Se ejecuta cada 5 segundos
            }else{
                JOptionPane.showMessageDialog(null, "Sistema no soporta systemTray", "Advertencia", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR);
        }
    }


//    Muestra una burbuja con la accion que se realiza
    public void MensajeTrayIcon(String texto, MessageType tipo) {
//        System.out.println("pasa por aqui");
        trayIcon.displayMessage("Advertencia.", texto, tipo);
//        System.out.println("texto: " + texto + " tipo " + tipo);
    }
//  Clase interna.
    class miTimerTask extends TimerTask {  

        @Override
        public void run() {
            //logica de la clase + llamada del metodo MensajeTrayIcon().
        }
    }
}

In this way I managed to show the message of my trayicon with certain actions in the background. I hope it serves you, thank you.

    
answered by 17.09.2017 / 22:11
source