¿Cómo puedo hacer para que mi JFrame
se inicie en segundo plano en el SystemTray
?
Tengo la clase Notificacion
la cual hago:
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;
Constructor:
public Notificacion(JFrame frame) {
this.parent = frame;
instanciarTray();
segundoPlano();
}
Métodos instanciarTray()
y segundoPlano()
:
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);
}
}
Pero cuando inicia mi aplicación no se ejecuta en segundo plano, ¿qué estoy haciendo mal?
Desde el JFrame
llamo a la clase de la siguiente forma:
Notificacion notificacion = new Notificacion(this);