aspose file tools*
The moose likes Beginning Java and the fly likes Please help with scope of panels... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Please help with scope of panels..." Watch "Please help with scope of panels..." New topic
Author

Please help with scope of panels...

Allen White
Greenhorn

Joined: Jul 01, 2002
Posts: 4
I'm not sure that's the best way to put my problem, but here goes.
If I construct a GUI element inside the init() method of my applet, how can I then access it's methods, since the variable referring to the object goes out of scope as soon as init() is done?
I need to construct it within init() because the element (a custom panel) contains canvas objects that depend on a MediaTracker object to ensure they size correctly for the images they receive.
Otherwise, the canvas objects go to their "preferred size" of 0,0 when the layout manager runs, and overriding getPreferredSize doesn't work because the images aren't loaded fully and return -1 for width and height.
Karl Laird
Ranch Hand

Joined: Jul 14, 2001
Posts: 67
You can declare the variable name as a global variable, and then construct the instance within the init() method. You dont have to intialise all variables when they are declared.


The Eagle sneers at the Peacock<p>Systems Administrator<br />OrderWare Solutions Ltd<br /><a href="http://www.orderware.net" target="_blank" rel="nofollow">http://www.orderware.net</a>
Allen White
Greenhorn

Joined: Jul 01, 2002
Posts: 4
Well, I assume you mean making the object reference static, as in :
static Panel newPanel;
...but that doesn't work. The run() method will show that the variable newPanel = null.
Karl Laird
Ranch Hand

Joined: Jul 14, 2001
Posts: 67
Why would you need to make it static? Static means that one instance of the variable is shared amongst all instances of a class - and Im assuming that isnt what you want
Karl Laird
Ranch Hand

Joined: Jul 14, 2001
Posts: 67
By global I mean to make the declaration of the variable ( Panel newPanel; ) outside of any method. You should then be able to reference the variable from any method inside the class (including in init() to go newPanel = new Panel(); )
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
AllenWhite
Welcome to the Java Ranch, we hope you�ll enjoy visiting as a regular however,
your name is not in keeping with our naming policy here at the ranch. Please change your display name to an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!


Dave
Stu Glassman
Ranch Hand

Joined: Jul 01, 2002
Posts: 91
Here's what Karl is trying to explain to you:
class MyApplet extends Applet
{
private Panel myPanel;
public void init() {
myPanel = new Panel();
}
}
-Stu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please help with scope of panels...