Minimize application to system tray

Asked

Viewed 1,071 times

1

I made an application as requested by a client, but at the end of the project he informed me that the application should open in Tray Icon, and when minimized continue running in Tray

My application is already fully developed, Tray icon does not need to have options, just stay minimized and while taking two clicks return to the main screen of the application.

I tried a few ways but I didn’t understand the operation.

How do I implement this?

  • What kind of application? Swing, javafx?

  • Application Swing @diegofm

  • Should it stay on Systemtray at all times? Even when visible, or minimized?

  • Only when @diegofm is minimized

1 answer

3


Basically you need to work with the classes SystemTray and window status, using WindowListeners.

With the first class you check whether the system supports adding applications to the system tray. Using the class TrayIcon, it is possible to create an icon that will be displayed. And in the second class you monitor when the window will be minimized, so that it is "iconified" to the tray, and when the window is reopened, so that the iconTray get out of there.

I made an executable example and commented on the most important points of the code.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;

public class FrameMinimizadoTest {

    public void start() throws IOException {
        final JFrame frame = new JFrame();
        frame.setTitle("Frame principal");
        frame.setSize(300, 200);

        JTextField field = new JTextField(10);
        frame.setLayout(new FlowLayout());
        frame.add(field);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //checa se o sistema tem suporte a iconTray
        if (SystemTray.isSupported()) {
            //pega uma instancia da bandeija do sistema
            final SystemTray tray = SystemTray.getSystemTray();
            //apenas para demonstração, altere para a imagem da
            //sua aplicação
            Image icon = ImageIO.read(new URL("http://www.freeiconspng.com/uploads/tick-icon-2.png"));
            //frame.setIconImage(icon);
            //cria um icone de bandeira, recebendo uma imagem 
            final TrayIcon trayIcon = new TrayIcon(icon);
            //IMPORTANTE! Deixa a propria API
            //decidir o tamanho, se remover essa linha
            //não aceitará imagem de qualquer tamanho
            trayIcon.setImageAutoSize(true);

            //adiciona uma ação ao frame, para monitorar alterações de status
            //da janela
            frame.addWindowStateListener(new WindowAdapter() {
                @Override
                public void windowStateChanged(WindowEvent e) {
                    //checa se a janela foi minimizada
                    if (e.getNewState() == JFrame.ICONIFIED) {
                        //listener para que a janela se abra com
                        //o clique do mouse
                        trayIcon.addMouseListener(new MouseAdapter() {
                            @Override
                            public void mouseClicked(MouseEvent e) {
                                frame.setVisible(true);
                                frame.toFront();
                                //remove o icone da bandeira
                                //quando a janela for reaberta
                                tray.remove(trayIcon);
                            }
                        });
                        try {
                            tray.add(trayIcon);
                        } catch (AWTException ex) {
                            ex.printStackTrace();
                        }
                        frame.setVisible(false);
                    }
                }
            });
        }
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                FrameMinimizadoTest f = new FrameMinimizadoTest();
                try {
                    f.start();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
}

References:

  • 1

    You’re the guy, I admire your willingness to help hehehehehe

  • @Viniciusleonardo we are there to really help :). Enjoy and take a look in that reply, If there’s anything wrong, let me know.

Browser other questions tagged

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