Launch my Windows application in System Tray

Asked

Viewed 1,565 times

10

I have no idea how I get my application to start with Windows 7 already in mode System Tray. That is, start with Windows and get the program icon next to the Windows 7 clock.

1 answer

9


Basically what you should do is take the instance of SystemTray of your operating system and add a new object of type TrayIcon customized to it.

The TrayIcon is the object you will work upon. After created, add an image to it, usually also added a menu so that there is a user interface.

To create this menu you must create a PopupMenu and add a MenuItem within it with the options you desire.

Example:

import java.awt.*;
public class MeuTray {
  public static void main(String args[]) {
    Runnable runner = new Runnable() {
      public void run() {
        if (SystemTray.isSupported()) {
          SystemTray tray = SystemTray.getSystemTray();
          Image image = Toolkit.getDefaultToolkit().getImage("imagem.gif");
          PopupMenu popup = new PopupMenu();
          MenuItem item = new MenuItem("Um MenuItem");
          popup.add(item);
          TrayIcon trayIcon = new TrayIcon(image, "Texto de dica", popup);
          trayIcon.setImageAutoSize(true); 
          try {
            tray.add(trayIcon);
          } catch (AWTException e) {
            System.err.println("Não pode adicionar a tray");
          }
        } else {
          System.err.println("Tray indisponível");
        }
      }
    };
    EventQueue.invokeLater(runner);
  }
}

Upshot:

Using the following image as icon:

imagem icone

Hovering the mouse over the icon:

mouse over

Right-clicking on the icon:

clicando no tray

Add more options to your Menuitem and handle events when the user selects the Menuitem item.

Sources:
Java Tutorials - How to Use the System Tray
Oracle Tech Tips - Getting to Know System Tray

  • It would be possible to put the functionality when right-clicking on the System Tray icon to open a menu?

  • @Junimentyes, see the last image, you’re already doing this >> "A Menuitem"

  • Thank you very much!! later I test. Congratulations on your level of knowledge! : D

  • @Thank you =) Although you have not tested I assure you it works, I tested this code from there to generate the images. If questions arise while implementing this code do not despair, it happens, but I’m sure you will implement as well.

  • Thank you. I’ve really enjoyed Java. Yes return if you need it. Ah! Thanks also for the attention! Hug!

  • Math, how do I add a new item to the menu? By selecting "Open" and "Quit" in the System Tray?

  • @I just saw your message. I just saw that you added more information here on the same topic, however the right one would be to create a new question, because the space below the question is only for answers. You could create a new question with the content you put down here?

  • You could hide the console window and only have Tray?

  • @Lucastorres hello, I believe so, but I would have to do some tests, I saw that you created a new question, I think it will be easier to get the answer so I will try to elaborate something here.

  • @Lucastorres running the code of my answer does not open any new window, only the command prompt remains open, if you want to be able to close it without killing the program run with javaw, I think this is all you need.

  • @Math I’ll specify in the other question how I’m running it, ok?

  • @Lucastorres ok

Show 7 more comments

Browser other questions tagged

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