Reputation: 8212
I have a WorldEditor
JFrame that launches a Game
JFrame. However, when the Game
closes, I don't want it to end the entire program, so I set the default close operation to HIDE_ON_CLOSE
. But, to save resources, I pause the WorldEditor
while the Game
is running.
How can I detect when the Game
window is hidden so I can resume WorldEditor
?
Upvotes: 1
Views: 1560
Reputation: 5874
Why don't you hide the frame yourself instead of using a default HIDE_ON_CLOSE
?
// inside WindowListener class
public windowClosing(WindowEvent e) {
yourFrame.setVisible( false );
// your code here...
}
Edit made: from docs:
The default close operation is executed after any window listeners handle the window-closing event. So, for example, assume that you specify that the default close operation is to dispose of a frame. You also implement a window listener that tests whether the frame is the last one visible and, if so, saves some data and exits the application. Under these conditions, when the user closes a frame, the window listener will be called first. If it does not exit the application, then the default close operation — disposing of the frame — will then be performed.
New edit with a working example:
import java.awt.event.*;
import javax.swing.JFrame;
public class ListenerTest extends JFrame implements WindowListener {
public static void main(String[] args) {
ListenerTest frame = new ListenerTest();
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setVisible( true );
}
public ListenerTest() {
this.addWindowListener( this );
}
public void windowActivated(WindowEvent e) {
System.out.println(" activated ");
}
public void windowClosed(WindowEvent e){
System.out.println(" closed ");
}
public void windowClosing(WindowEvent e){
System.out.println(" closing ");
}
public void windowDeactivated(WindowEvent e){
System.out.println(" deactivated ");
}
public void windowDeiconified(WindowEvent e){
System.out.println(" deiconified ");
}
public void windowIconified(WindowEvent e){
System.out.println(" iconified ");
}
public void windowOpened(WindowEvent e){
System.out.println(" opened ");
}
}
Test this out in order to catch what which events are firing.
Upvotes: 5