Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JFrame + windowClosed event is not getting fired.

 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
on button click event, I'm creating the new JFrame instance for showing some Java 3D output.
But the code for windowClosed() method doesn't get executed at all,
but if I provided

for the same frame instance, then there is no problem

Note: J3DContainerFrame extends JPanel
Any pointers ?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try using the method hinted at by your sys.out.println

//public void windowClosed(WindowEvent e)
public void windowClosing(WindowEvent e)
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The windowClosed event is fired when the window has already closed. But it will not close if you allow it to close. You can do this by setting a proper default close operation. When you click the X it will fire the windowClosing event instead.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, that was a silly mistake, Next time I should look more carfully in Java Docs
Thanks Michael & Rob for the quick and correct reply.

But my little knowledge of JFC forces me to ask this:
Why we need (separate) "windowClosed" event, can't we simply write all the code/logic related to "window closing/ed" thing in one method ?
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you may want to be informed of both when the window is about to be closed (windowClosing), perhaps so you can block it, and when the window is actually closed (windowClosed), perhaps so you can do some clean-up.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm, convincing.. I got it.
 
Rob Camick
Ranch Hand
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
windowClosing is always fired.

windowClosed will be fired when you use:



I tend to use windowClosing as suggested in the Closing An Application entry.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic