How to add an event to the Trayicon notification balloon?

Asked

Viewed 631 times

1

I have an app that uses the class TrayIcon and I’m displaying a message through the method displayMessage(). (A small "balloon" that rises from the icon represented by my Trayicon).

The method call is as follows:

tray.displayMessage("My Title", "My Message", TrayIcon.MessageType.WARNING);

Where tray is my type object TrayIcon.

But when I click on this "little balloon" nothing happens. In fact, the "little balloon" is closed. But I need to do something specific when this message is clicked.
As I add a mouse event click on this message?

If that’s not possible, can someone suggest me some alternative to solve my problem?

Follows an excerpt from the code:

import java.awt.AWTException;   
import java.awt.Image;    
import java.awt.SystemTray;  
import java.awt.Toolkit;  
import java.awt.TrayIcon;

public class Principal {

    Principal() {
        initialize();
    }

    private void initialize() {
        Image trayImage = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("icon.png"));
        TrayIcon tray = new TrayIcon(trayImage, "Tray Icon Example");
        SystemTray sysTray = SystemTray.getSystemTray();
        try {
            sysTray.add(tray);
        } catch (AWTException e) {
            e.printStackTrace();
        }
        tray.displayMessage("Atenção!", "Clique aqui para abrir mais detalhes.", TrayIcon.MessageType.WARNING);
    }

    public static void main(String[] args) {
        new Principal();
    }

}
  • It’s desktop application in swing?

  • Fernando, Welcome to Stack Overflow. An example of code makes it easier to help you (have a look at How to create a Minimum, Complete and Verifiable example) in Help Center. Anyway, you can add a ActionListener or MouseListener at the Trayicon, said this be aware that this will not work on all desktops / platforms.

  • Yeah, it’s a desktop swing app. I use the following to display this message: Tray.displayMessage("My Title", "My Message", Trayicon.MessageType.WARNING); Obs.: "Tray" is my Trayicon object. If I add an event to my Trayicon object, the event will work for the whole object, but that’s not the idea. The "little balloon" appears and disappears after a few moments. By clicking on this message it disappears, but I need it to perform a proper action (which I have already developed). But I don’t understand how to do this.

  • Add a [mcve] code to your question, so you can simulate this function of your application and suggest a solution.

  • I edited the question including an example as requested. I needed to take an image from the internet to treat it as "icon.png", I couldn’t find a standard Java Image object.

1 answer

3


Add a ActionListener in his TrayIcon:

tray.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //ação que vai ocorrer quando o balão
               //for clicado
            }
        });

See implemented in an executable example:

import java.awt.AWTException;   
import java.awt.Image;    
import java.awt.SystemTray;  
import java.awt.Toolkit;  
import java.awt.TrayIcon;

public class Principal {

    Principal() {
        initialize();
    }

    private void initialize() {
        Image trayImage = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("icon.png"));
        TrayIcon tray = new TrayIcon(trayImage, "Tray Icon Example");
        SystemTray sysTray = SystemTray.getSystemTray();
        tray.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "clicou");
            }
        });
        try {
            sysTray.add(tray);
        } catch (AWTException e) {
            e.printStackTrace();
        }
        tray.displayMessage("Atenção!", "Clique aqui para abrir mais detalhes.", TrayIcon.MessageType.WARNING);
    }

    public static void main(String[] args) {
        new Principal();
    }
}

See working:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.