Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing a JFrame programmatically

Status
Not open for further replies.

SSJ

Programmer
Sep 26, 2002
54
0
0
PT
Hi.
I'm developing an application designed to work in touch screens. Therefore I need to use JButtons to close the JFrames I use.
The question is how can I simulate a click on the JFrame close button?

So far what I'm doing is call this.dispose() when the user clicks my button.
The frame closes, but if the user opens it again all the data that was in the text boxes and other components will appear again. If I click on the close button the frame will close and if the user opens the frame again everything comes with no data as it should.

The default close operation of my JFrame is also dispose on close.

What seems to me is that when I call dispose the garbage collector doesn't remove the references to my JFrame and therefore if its opened again it comes with all the data that was in it. If I press the JFrame close button the garbage collector automatically cleans the memory references to the JFrame and therefore if the user opens it again it will come with no data.

So how exactly should I do this?

TIA
 
// try this code
// if you didn't uncomment line: textField = new JTextField();
// data in textField will not be clear

// uncomment line: textField = new JTextField();
// and try again, you will got new child with no data in textFiled

import javax.swing.*;
import java.awt.event.*;

public class C extends JFrame{
JButton button;
JFrame child;
JTextField textField = new JTextField();
public C(){

button = new JButton("create frame");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
child = new JFrame("child");
//textField = new JTextField();
child.getContentPane().add(textField);
child.pack();
child.show();
}
});
this.getContentPane().add(button);
}
public static void main(String args[]){
C c = new C();
c.pack();
c.setLocationRelativeTo(null);
c.show();
}
 
hologram: That would finish my application for good, thats not what I want...

roong: That solution also doesn't work since like I said the constructor isn't called when I open the JFrame again.

Isn't there any way to programmatically simulate a click on a JFrame close button?
 
>> So how exactly should I do this?

Hide the JFrame
Set your reference to "null"
When the user clicks "Open" the next time create a "new" frame object.

-pete
 
Set the reference to null?

at this time what I have is:

bt_Close_action_performed() {
this.dispose();
}

how can I set the reference to null here, I can't do this = null.
 
if you use dispose()
when you show frame again that frame will appear same as before you called dispose()

I think you should clear data in Object(s) by yourself before call dispose() or affter you show its again.
 
From the Sun API doc (jdk1.3.1) :
Code:
public void dispose()
"Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be marked as undisplayable.
The Window and its subcomponents can be made displayable again by rebuilding the native resources with a subsequent call to pack or show. The states of the recreated Window and its subcomponents will be identical to the states of these objects at the point where the Window was disposed (not accounting for additional modifcations between those actions)."

==> So use Palbano's or Roong's approach.

 
>> how can I set the reference to null here, I can't do this = null.

I don't have your code so i can't tell you where your reference is. As a programmer it falls to you to understand your Object Oriented Design and the interaction of your classes.

-pete
 
SSJ,

Can you post your code (the bare essentials)? It'll be a lot easier to help you.
 
nevermind, it was simple. The problem was that this system is being developed by several persons not all at the same country... and some guy was storing all the references to opened frames in a cache, and wasn't removing them properly(thats why my constructor wasn't being called after the frame was opened the first time).
The dispose method works fine, my code was working. Can't believe the time I wasted with this though :|

Thanks to all for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top