Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Task tray notification balloon events?

Is there any java event that could be called on the task tray icon notification balloon, that is

trayIcon.displayMessage(title, message, TrayIcon.MessageType.INFO)

like dropbox uses, when I click on the balloon it take me to the folder where it had downloaded the recent file. Is it possible using java?

like image 557
NoNaMe Avatar asked Dec 04 '22 13:12

NoNaMe


1 Answers

By doing some R&D I got that tray icon double click event get called when we click on the notification balloon. This is what I was looking for.

Check this code:

public class Main {
  static Image image = Toolkit.getDefaultToolkit().getImage("images/tray.gif");

  static TrayIcon trayIcon = new TrayIcon(image, "Tester2");

  public static void main(String[] a) throws Exception {
    if (SystemTray.isSupported()) {
      SystemTray tray = SystemTray.getSystemTray();

      trayIcon.setImageAutoSize(true);
      trayIcon.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          System.out.println("In here");
          trayIcon.displayMessage("Tester!", "Some action performed", TrayIcon.MessageType.INFO);
        }
      });

      try {
        tray.add(trayIcon);
      } catch (AWTException e) {
        System.err.println("TrayIcon could not be added.");
      }
    }
  }
}

and I modified it for me in this way.

private static String location = Roor_Location_Here// contain the root location

Then in the double click event

/*
 * Double click action performed *
 */
trayIcon.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            Desktop dst = Desktop.getDesktop();
            dst.open(new File(location));
            /*
             * reset the location to root location again.
             */
            location = Root_Location_Here // again setting root location                    
        } catch (Exception ex) {
            logger.error("------- error with folder opening double click "
                    + ex.getMessage());
        }
    }
});

And this is the function I use to call for notification balloon to show the message:

public static void showNotification(String title, String msg,
            String location) {
    if (SystemTray.isSupported()) {
        trayIcon.displayMessage(title, msg, TrayIcon.MessageType.INFO);
        SystemTrayGUI.location = location;// this sets the location that should be opened when balloon is clicked.

    }
}
like image 97
NoNaMe Avatar answered Dec 31 '22 13:12

NoNaMe