how can multiple JFrames all have the same name? (previous post rephrased)

R

rfractal30

Hi

I've got a question about JFrames. In a UI I created I have a button
which launches JFrames (actually they are extentions of JFrame:
ResizeFrame), each of which has the same name: 'jf'. Each JFrame
contains a panel - which is in turn displays an Image. The JFrames can
be opened to display adifferent Images (if the user so wishes).

What I really want to know is this - how is it possible to have several
JFrames opened at once, all of which have the same name: 'jf'?

What happens is that the user clicks a button, and the following code
is run:

//---
ResizeFrame jf = new ResizeFrame((w + " - Depth Map"), new Color(205,
219, 227), false);
jf.LoadPic(w);
jf.addpanel();
//---

The user can click the button any number of times - created multiple
instances of the object 'jf'. When the user closes the frame it is
dispose()d.

I've got a suspician that there is a superficial appearance of objects
being allocated, but that in actual fact these are not really allocated
objects. Either that or only the last JFrame created is allocated to
'jf' while the previous JFrames become unallocated.

I have posted a similar question a little while back, however I am now
clearer about what it is I am trying to understand - and have therefore
found a better way to phrase my question!

Any advice would be greatly appreciated.

Michael
 
R

rfractal30

Ok. I got the answer..

It turns out that this was an issue to do with scope. What happens is
that the reference to the JFrame (i.e. 'jf') disappears after the
button push (because it is out of scope - same as any kind of method
call). However the object itself is still in existence. With most types
of objects the garbage collector will pop by and clean up the nameless
object - however, with frames it is different - they have to be
physically removed using a dispose() call. Take the following code:

public class TestScope {
public static void main(String[] args) {
TestScope ts = new TestScope();
ts.printString();
ts.printString();
}
public void printString(){
String ps = new String("hi");
System.out.println(ps);
}
}

Here we can see that the method printString is called twice. Each time
the String object ps exists only while the method is being exucuted. As
soon as the method has finished doing all it's stuff then the reference
'ps' vanishes - however the String object it is referring to
(containing the word "hi") hangs around for a while until the garbage
collector clears it up. If this were a JFrame object and not a String,
then the object will hang around indefinitely until it is dispose()d.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top