This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help with scope of panels...

 
Allen White
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Allen White
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3244
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
Stu Glassman
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic