0

I have an app that checks password and should minimize to system tray if password is correct. But I don't know how to set frame invisible or minimize it using ActionListener.

public class MainWindow extends JFrame {
    private JPanel panel1;
    private JTextField Password;
    private JButton readyBtn;
    private JLabel label1;
    private JLabel label2;
    int count = 0;

    public MainWindow() {
        readyBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String password = Password.getText();
                if (password.equals("12345")) {
                    //minimize to system tray
                }
            }
        });
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("MainWindow");
        frame.setContentPane(new MainWindow().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        //frame.setUndecorated(true);
        frame.pack();

        TrayIcon trayIcon = new TrayIcon(Toolkit.getDefaultToolkit().getImage("res/lock.png"));
        trayIcon.setToolTip("Running...");
        trayIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                frame.setAlwaysOnTop(true);
                frame.setVisible(true);
                JOptionPane.showMessageDialog(null, "Clicked");
            }
        });

        try {
            SystemTray.getSystemTray().add(trayIcon);
        }catch (Exception e){
            System.out.println(e);
        }
    }
}
Alina
  • 424
  • 1
  • 4
  • 17
  • 1
    Possible duplicate of [How to hide a JFrame in system tray of taskbar](https://stackoverflow.com/questions/7461477/how-to-hide-a-jframe-in-system-tray-of-taskbar) – M. Prokhorov Jan 23 '18 at 14:02

1 Answers1

1

you can use

frame.setExtendedState(JFrame.ICONIFIED);
Omega Kewt
  • 138
  • 3
  • 9