Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Minimize to tray on close button click.

 
rahul S Sharma
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of System.exit(0); use setVisible(false);
 
Campbell Ritchie
Sheriff
Pie
Posts: 49462
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or find the method which minimises the GUI. It may be called setIconified, or similar.
 
Rob Spoor
Sheriff
Pie
Posts: 20555
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20555
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're completely right. I missed that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic