1

My application has a JFrame and checks every x seconds if something changed. So I would like to hide my JFrame via setVisible(false) on a click on the close button and redisplay it when the icon in the dock (I'm using Mac OS, but it should work the same way with the Windows task bar) is clicked. You know: many applications do this temporary hiding.

Have you got any ideas how to do this? How to listen on these click events?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
mythbu
  • 657
  • 2
  • 7
  • 16
  • 2
    Take a look at `JFrame#setExtendedState`, passing it `JFrame#ICONIFIED`, which should "minimize" the frame, allowing it to be automatically restored by the OS – MadProgrammer Jan 26 '13 at 11:35
  • This brings the frame only in the dock. That means: the dock contains then the icon of the program and the small program window. – mythbu Jan 26 '13 at 11:45
  • AFAIK, when you use `setVisible(false)` in Windows, all the icons related to that running `JFrame` also get lost, as seen in this [answer](http://stackoverflow.com/a/9374398/1057230). This [thread](http://stackoverflow.com/q/7461477/1057230), does provide some help on the said thingy. – nIcE cOw Jan 26 '13 at 13:07

2 Answers2

2

Here is a little sample, how to hide/open window in the tray.

import java.awt.Image;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class Test {

    public static void main(String[] args) throws Exception {
        final JFrame frm = new JFrame("Test");
        Image im = Toolkit.getDefaultToolkit().getImage("c:\\icons\\icon1.png");
        final TrayIcon tri = new TrayIcon(im);
        tri.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                frm.setVisible(true);
                try {
                    SystemTray.getSystemTray().remove(tri);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        frm.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                try {
                    SystemTray.getSystemTray().add(tri);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                frm.setVisible(false);
            }
        });
        frm.setSize(100, 100);
        frm.setVisible(true);
    }
}
Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48
0

Use the com.apple.eawt or java.awt.Desktop packages to listen to Events that occur when the application is closed, hidden or reactivated.

Particularly com.apple.eawt.AppReOpenedEvent is cast when the Dock Icon is clicked. When you handle the event with com.apple.eawt.AppReOpenedListener, set the frame visible again:

@Override
public void appReOpened(AppReOpenedEvent arg0) {
    invalidate(); // Suppose these are optional, but make sure the layout is up to date
    pack();
    validate();
    setVisible(true);
}
nhaggen
  • 301
  • 2
  • 8