Tomáš Zato
Tomáš Zato

Reputation: 53129

Show JFrame when tray icon was clicked even if minimized

I need to restore a hidden window when tray icon is clicked. I've actually already found partial solution:

tray_icon.addMouseListener(new MouseListener() {
  @Override
  public void mouseClicked( MouseEvent e ) {
    //Gui.this refers to my frame
    Gui.this.setVisible(true);
  }
});

This will show the frame if it's behind another window. It will put the frame on top. But if I minimize the frame, it doesn't show the window. It's interesting however, that it slightly highlights the taskbar tab:

image description

Taskbar flashing is nice, but it's not enough:

Note that I plan to allow "minimize to tray" function. This means I'll be even hiding the window completely (provided Java allows it). It still must be possible to show it.

Upvotes: 0

Views: 2264

Answers (1)

Tomáš Zato
Tomáš Zato

Reputation: 53129

Along with setVisible, there's other thing to be set:

Gui.this.setState(Frame.NORMAL);

When minimized, the frame's state is Frame.ICONIFIED.

This is the complete callback to restore the hidden frame:

   tray_icon.addMouseListener(new MouseListener() {
     @Override
     public void mouseClicked( MouseEvent e ) {
       Gui.this.setVisible(true);
       Gui.this.setState (Frame.NORMAL);
     }
   }

And this is what I use to hide the window and taskbar panel (minimize to tray):

 this.addWindowListener(new WindowAdapter()
 {
    @Override
    public void windowIconified(WindowEvent event) {
      //Hides it from screen
      Gui.this.setState(Frame.ICONIFIED);
      //Hides it from taskbar and screen
      Gui.this.setVisible(false);
    }
 });

Upvotes: 3

Related Questions