Forums Register Login

Minimize to tray on close button click.

+Pie Number of slices to send: Send
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....
+Pie Number of slices to send: Send
Instead of System.exit(0); use setVisible(false);
+Pie Number of slices to send: Send
Or find the method which minimises the GUI. It may be called setIconified, or similar.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
You're completely right. I missed that.
If I'd had more time, I would have written a shorter letter. -T.S. Eliot such a short, tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 4000 times.
Similar Threads
When click on minimize button then it must goes to System Tray?
questions regarding system tray and tray icon
tray icon is not displaying
How do you get the JButton name
PlEASE, HELP!
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 08:21:05.