• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

JFrame ?!!!

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I close / destroy a JFrame from another JFrame ?
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should be able to if you have a reference to the frame in question. For example, if you want to close a frame myFrame, you could do it like this:
<PRE>
myFrame.hide();
myframe.destroy();
</PRE>
You should be able to do this anywhere that myFrame is a valid reference.
Nick
 
Siva Jagadeesan
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nick Riviera:
You should be able to if you have a reference to the frame in question. For example, if you want to close a frame myFrame, you could do it like this:
<PRE>
myFrame.hide();
myframe.destroy();
</PRE>
You should be able to do this anywhere that myFrame is a valid reference.
Nick


So if I do have a Reference of the JFrame frame1 and if I have a method like this in the class which extends frame1
public void actionPerformed(ActionEvent e){
//Showing another frame2
frame2.pack();
frame2.setVisible(true);
frame1.hide();
frmae1.destroy();
}
Do u think this method is correct ?

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, my bad. The method is dispose(), not destroy(). Calling dispose() will release the resources that the jFrame is holding on to. Then you can set the reference to null and it will be garbage collected. You can even garbage collect it explicitly if you desire.
Anyway, the short answer is, "Yes, your code should work, if you replace destroy() with dispose()". You may also try something like this:
<PRE>
frame.hide();
frame.dispose();
frame = null;
Runtime.getRuntime().gc();
</PRE>
Nick
 
Siva Jagadeesan
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you it works
 
reply
    Bookmark Topic Watch Topic
  • New Topic