Minimize application for the system tray

1

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

My application is already fully developed, tray icon does not need to have options, just to be minimized and to give two clicks return the main screen of the application.

I tried in some ways but I did not understand the way it works.

How do I implement this?

    
asked by anonymous 15.07.2016 / 15:40

1 answer

3

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

With the first class you check if the system supports adding applications to the system tray. Using the TrayIcon class, you can 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" for the tray, and when the window is reopened, so that the iconTray leaves 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:

15.07.2016 / 17:03