My question is that the reference b is assigned Button object three times...and the first two buttons "Hello" and "OK" are not referenced by any references..
Now as they are not referenced by any reference, so garbage collector may collects its memory, resulting in loss of these two buttons.....
Am I right or not???
Are you *sure* nothing references them? Does anything *else* reference them?
Inder Kumar Rathore
Ranch Hand
Joined: Apr 14, 2010
Posts: 41
posted
0
it shows three buttons.....and yes there is no reference for first two buttons....
for your simplicity run this one....
import java.awt.*;
import java.awt.event.*;
public class Activator {
public static void main(String[] args) {
Button b;
Frame f = new Frame("Hello Java");
f.add(b = new Button("Hello"),BorderLayout.NORTH);
f.add(b = new Button("OK"), BorderLayout.CENTER);
f.add(b = new Button("Quit"),BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
}
Inder Kumar Rathore
Ranch Hand
Joined: Apr 14, 2010
Posts: 41
posted
0
in the latest code I removed the event handling.....in UI there are three buttons and when i click on them nothing happen...the buttons are still present...
and in former code where I have used event handling everything is working fine........
Ernest Friedman-Hill
author and iconoclast
Marshal
The Frame that contains them is referencing them. When you call add(), the Frame adds the Button to a collection; that collection keeps the Button from being garbage collected. The only reason for using the variable "b" in the original code is so that the event listener can be added to the button.
Because the event queue (and perhaps other internal parts of the GUI) has a reference to the Frame. To null out all those references you have to call dispose() on the Frame; then it can be garbage-collected.
Inder Kumar Rathore
Ranch Hand
Joined: Apr 14, 2010
Posts: 41
posted
0
thanks for your reply
Mark E Hansen
Ranch Hand
Joined: Apr 01, 2009
Posts: 639
posted
0
What you may be missing here is that you're creating components for use by the AWT. When you call setVisible on the frame, the AWT event thread takes over and manages the application.
Have you wondered what happens to the application when the main() method is finished? Perhaps you haven't noticed, but that method finishes and the original thread that started it is done. The application then run via the AWT event thread.
I mention this because of your comment that one of your 'b' variables was still set. In actuality, after main() ends, all the local variables are out of scope (both 'b' and 'f').
By the way, for a simple test application, it's okay to create graphical components in the main application thread. However, you should get in the habit of creating all graphical components in the AWT event thread, using EventQueue.invokeLater(...); Here is an example:
Inder Kumar Rathore
Ranch Hand
Joined: Apr 14, 2010
Posts: 41
posted
0
Who is the parent thread of Awt event thread, JVM or main method.....
Unknown. A little investigation has shown me that the EDT is member of the main thread group, of which the main thread is also a member. That doesn't necessarily mean that the main thread is also the parent thread - there are a few other member threads. But does it really matter?