Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Minimize to tray instead of close  RSS feed

 
Mattias Johansson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a better solution to this then just not closing down the application when the user presses the X in the windows upper right corner?
Not that it matters but I'm using the TrayIcon class from Java 6 for the tray icon.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can set the default close action to Frame.DO_NOTHING_ON_CLOSE, then use a WindowListener to listen for the windowClosing event. If this occurs, just call setVisible(false) on the frame. This requires that you use the same frame throughout the program life span; if you create a new frame each time, use dispose() instead of setVisible(false).
 
Mattias Johansson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the expertise!
But isn't setVisible(false) invoked automatically when the default close operation is not set or set to DO_NOTHING_ON_CLOSE? In my current test application I didn't set the default close operation, but it still worked to display it again using setVisible(true).
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
setVisible(true) also builds the UI if necessary, so if you had disposed it it will still be visible.

You are right however on the default default-close-operation. I thought it was DISPOSE_ON_CLOSE by default, but it is actually HIDE_ON_CLOSE.

You just have to make sure that the application doesn't close when the frame is hidden. By default it won't, so I guess just pressing X will do perfectly.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
setVisible(true) also builds the UI if necessary, so if you had disposed it it will still be visible.

You are right however on the default default-close-operation. I thought it was DISPOSE_ON_CLOSE by default, but it is actually HIDE_ON_CLOSE.

You just have to make sure that the application doesn't close when the frame is hidden. By default it won't, so I guess just pressing X will do perfectly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!