File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Minimize to tray on close button click. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Minimize to tray on close button click." Watch "Minimize to tray on close button click." New topic
Author

Minimize to tray on close button click.

rahul S Sharma
Greenhorn

Joined: Jun 08, 2011
Posts: 21
Hi all,

I have made a jframe in which i am using setDefaultCloseOperation(HIDE_ON_CLOSE) operation. By this when i click on close button of window, frame simply hide and my tray icon doesn't disappear but when i click on Jbutton named as CLOSE, tray icon disappear because i have written System.exit(0) in button action performed method. Now I want that when i click on CLOSE Jbutton, tray icon should not disappear from its position.

regards....
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10489
    
    9

Instead of System.exit(0); use setVisible(false);


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Or find the method which minimises the GUI. It may be called setIconified, or similar.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

It's setExtendedState, with the state set to x | Frame.ICONIFIED where x is the current extended state. To restore, you then call setExtendendState with the state set to x & ~Frame.ICONIFIED with x again being the current extended state. By using this approach, the frame will go back to the previous state - maximized or not.

However, this will still show the application in the task bar. You will need to call setVisible(false) as well. The best way to handle this:
- minimize by clicking the icon in the frame's title bar: use a WindowListener that calls setVisible(false) in the windowIconified method.
- minimize from code: first use setExtendedState as I described above, then call setVisible(false).
- restoring from code: first call setVisible(true), then use setExtendedState as I described above.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Rob Spoor wrote:
- minimize by clicking the icon in the frame's title bar: use a WindowListener that calls setVisible(false) in the windowIconified method.
- minimize from code: first use setExtendedState as I described above, then call setVisible(false).


Haven't tested, but the WindowListener's windowIconified(...) should be called regardless of whether the window was iconified/minimized by user action or code execution. So the bolded part shouldn't be needed.

luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

You're completely right. I missed that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Minimize to tray on close button click.