My question is how can I get a msg from the taskbar by calling my method MessageTrayIcon () from another class? I mean, I call it but I do not do any action, msj does not appear in my taskbar, I have a Timer that fires every X seconds, msj should check if there is something to show that appears in the notification bar but that do not do anything.
I have my MessageTrayIcon method that is in my class notification.
public class Notificacion {
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(), 2000, 5000);//Se ejecuta cada 5 segundos
}
} 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) {
trayIcon.displayMessage("Advertencia.", texto, tipo);
System.out.println("pasa por aqui");
}
}
This method is called from another class which does a series of things which compares if it finds matches between the contents of a Map and a arraylist should show a msg in the notification bar in the case that it has something to show but he does not.
public class LeerArchivos {
Notificacion notificacion = new Notificacion();
ArrayList<String> keyArray = new ArrayList<>();
public void leer(ArrayList<String> arrayList) {
Map<String, String> mapaCodigosArchivo = new HashMap();
try {
// Abrimos el archivo con la ruta especificada.
FileInputStream fstream = new FileInputStream(new File("Sucursales.txt"));
// Creamos el objeto de entrada
DataInputStream entrada = new DataInputStream(fstream);
// Creamos el Buffer de Lectura
BufferedReader buffer = new BufferedReader(new InputStreamReader(entrada));
String contenido;
// Leer el archivo linea por linea
while ((contenido = buffer.readLine()) != null) {
String[] separador = contenido.split(",");
mapaCodigosArchivo.put(separador[0], separador[1]);
}
keyArray.clear();
//Recorremos el arrayList
for (String nombreArchivo : arrayList) {
String[] separador = nombreArchivo.split("_");
String codSucursal = separador[0].replace("PDA", "").trim();
keyArray.add(codSucursal);
}
for (String key : mapaCodigosArchivo.keySet()) {
int encontrados = this.contarTicketPorSucursal(keyArray, key);
if (encontrados > 0) {
notificacion.MensajeTrayIcon("Tienes " + encontrados + " ticket pendiente de la sucursal: " + mapaCodigosArchivo.get(key), MessageType.INFO);
// System.out.println("Tienes " + encontrados + " ticket pendiente de la sucursal: " + mapaCodigosArchivo.get(key));
}
}
// Cerramos el archivo
entrada.close();
} catch (Exception e) { //Catch de excepciones
System.err.println("Ocurrio un error: " + e.getMessage());
}
}
private int contarTicketPorSucursal(ArrayList<String> keyArray, String key) {
int contador = 0;
for (int i = 0; i < keyArray.size(); i++) {
if (keyArray.get(i).equals(key)) {
contador++;
}
}
return contador;
}
}
If I debug the MessageTrayIcon method by printing it on the screen, it calls it, it goes through the cod but it does not appear in the taskbar, I mean? How could I make it so that I said msj ..
Thank you very much for your help.