*
The moose likes Swing / AWT / SWT and the fly likes JFrame confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFrame confusion" Watch "JFrame confusion" New topic
Author

JFrame confusion

Chris Pat
Ranch Hand

Joined: Aug 27, 2005
Posts: 156
Hello
I have a JFrame in object2 that on button click creates and launches object1 run method. Two questions. One. Can I create a new JFrame in object1 run method and and have it persist after run finishes? Two. How can I create the JFrame in object 2 so that I can add to it from object1 run method? Still learning. tia.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
There are different ways of doing this. Here's one possibility.
Chris Pat
Ranch Hand

Joined: Aug 27, 2005
Posts: 156
Thank you. That was extremely instructive.
Is it possible to create an independent window from within object2 run method? If not why not? Thank you again. Nothing better than deeply learning even something small for the first time.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Is it possible to create an independent window from within object2 run method?
Yes. Generally we try to have only one top–level container in an app. For other windows we use dialogs. Be careful not to use modal dialogs, including all JOptionPane dialogs, in the run method or it will block the run thread. If you elect a JDialog you could keep a reference to it as a member variable, instantiate it in or via the class constructor and set it visible in the run method. In java:
Ricky Clarkson
Ranch Hand

Joined: Jul 27, 2006
Posts: 131
It's possible to create any object within any method. The object will live at least until there are no more references to it. For Frames (JFrames are Frames), all instances are referred to by a static field in the Frame class, so they will live at least as long as you haven't called dispose() on them.

There is a difference between objects and variables, that I'm suspecting you haven't realised.



The variable, frame, lives exactly as long as it takes for doSomething to complete, i.e., not very long. It is a "lexically-scoped variable", which is an overblown way of saying that the name only exists within the { and the } it's declared in.

The actual JFrame object that's created, which doesn't have a name, outlives the method. This is usually not very useful; you tend to want a name to access an object by, so that you can do things with it. One idea is to make doSomething return the JFrame:



Then you can keep a reference somewhere else:

theFrame=doSomething();

One way to think of objects and variables is as stellar bodies, and names that humans give to them. To one person, a star may be Betelgeuse, to another it may be "that bright one up there". That's like having two variables both pointing at the same object. If the name Betelgeuse stops being used, that doesn't make the star disappear.
sudhakar ananth
Ranch Hand

Joined: Feb 02, 2007
Posts: 68
i too have the same problem the thing is the JOptionpane is pop up and i need the Jframe to which is main application here should be running if the JOptionPane is there contin...

pls help me out it is urgent....
sudhakar ananth
Ranch Hand

Joined: Feb 02, 2007
Posts: 68
I have the JOptionpane which is poped up when i click some button in JFRAME and i need the Jframe to (which is main application here) should be running
if the JOptionPane is there contin. ... on the screen .
i want the JFRAME application to be working even though the JOPTION PANE is in the on the SCREEN

pls help me out it is urgent....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JFrame confusion