Minimizar + Sytemtray

Asked

Viewed 356 times

3

People are wanting that when clicking to minimize the program, its icon is next to the clock. I created a button that does this action. I confess that I didn’t do it, but the author helped me a lot.

That’s the code I’m using on the button. I wish I could do the same thing mimicking the program to be next to the clock without having to click the button I created.

Thank you in advance!

if (SystemTray.isSupported()) {
    final SystemTray systemTray = SystemTray.getSystemTray();
    final TrayIcon trayIcon = new TrayIcon(new ImageIcon(icoPath, "omt").getImage(), "QuickStage");
    trayIcon.setImageAutoSize(true);// Autosize icon base on space

    // Mouse
    MouseAdapter mouseAdapter = new MouseAdapter() {

        // Exibir
        @Override
        public void mouseClicked(MouseEvent e) {
            systemTray.remove(trayIcon);
            main.this.setVisible(true);
        }
    };

    // Ocultar
    trayIcon.addMouseListener(mouseAdapter);
    try {
        systemTray.add(trayIcon);
        main.this.setVisible(false);
        trayIcon.displayMessage("Aviso!", "QuickStage continua em execução...", TrayIcon.MessageType.INFO);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

1 answer

6


The answer is already in your code. To make it easier, we will rearrange it:

public void moveToTray() {
    if (!SystemTray.isSupported()) return;
    final SystemTray systemTray = SystemTray.getSystemTray();
    final TrayIcon trayIcon = new TrayIcon(new ImageIcon(icoPath, "omt").getImage(), "QuickStage");
    trayIcon.setImageAutoSize(true);// Autosize icon base on space

    // Mouse
    MouseAdapter mouseAdapter = new MouseAdapter() {

        // Exibir
        @Override
        public void mouseClicked(MouseEvent e) {
            systemTray.remove(trayIcon);
            main.this.setVisible(true);
        }
    };

    // Ocultar
    trayIcon.addMouseListener(mouseAdapter);
    try {
        systemTray.add(trayIcon);
        main.this.setVisible(false);
        trayIcon.displayMessage("Aviso!", "QuickStage continua em execução...", TrayIcon.MessageType.INFO);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

With this, just call the method moveToTray() whenever you want to minimize the program on Tray, including your button code:

botao.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        moveToTray();
    }
});

Or if you have Java 8:

botao.addActionListener(e -> moveToTray());
  • Good evening... I’m trying in every way to implement a Trayicon... It works perfectly this example, but I do not know what I’m doing that my image does not appear. There is an empty space, it shows the message and also the warning when you put the mouse up but the image does not appear. Have you been there? Could you help me? The image I put "JPG,PNG,GIF,..." The size this 16X16 px.

  • @Valdecir I suggest you create a new question for this, including all the pertinent source code you have.

Browser other questions tagged

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