| Author |
i want to call main class constructor from inner class or smt...
|
Juhan Voolaid
Ranch Hand
Joined: Nov 18, 2003
Posts: 179
|
|
Hi I have a simple problem, but i don't know how to solve it. The situation is: Program has main class Hero and the constructor Hero() where are several panels. One panel has buttons. Each panel is defined in inner class. Now i want to do so that, if i press button from lets say buttonPanel i want to see something happening in other panel - lets say actionPanel. Problem is that when i press button - actionPanel must be refreshed - to do that i must call actionPanel constructor i think. Hohh ... this is creazy to explain ... i write a little code below ... try to make sense of that and correct me I hope u get the idea and know to give me some advice or something....
|
 |
Craig Wood
Ranch Hand
Joined: Jan 14, 2004
Posts: 1535
|
|
One way to do this is to give (pass) an ActionPanel reference (namely, actionPanel) to the ButtonPanel class so ButtonPanel can call methods inside ActionPanel. So we pass the reference to the constructor when we instantiate (create an instance of) ButtonPanel. Now the buttons in ButtonPanel can use the reference to call methods inside ActionPanel.
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
|
Moving this to the Swing / JFC / AWT forum, where folks just love to get all messy with this GUI stuff...
|
[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
|
 |
Juhan Voolaid
Ranch Hand
Joined: Nov 18, 2003
Posts: 179
|
|
Thank you, thank you very much for such a good answer to such a messy question Craig Wood!!! I feel i jsut gained a level in Java because of u
|
 |
Juhan Voolaid
Ranch Hand
Joined: Nov 18, 2003
Posts: 179
|
|
One other thing .... what does this line this.actionPanel = actionPanel; do in this example? [ February 15, 2004: Message edited by: Juhan Voolaid ]
|
 |
Nathan Pruett
Bartender
Joined: Oct 18, 2000
Posts: 4121
|
|
|
It assigns the value of local variable named "actionPanel" (that was passed in as a parameter to the constructor), to the member variable named "actionPanel" that is defined asa part of the ButtonPanel class. The "this" keyword lets you access object-level or class-level attributes and methods of the current object.
|
-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
|
 |
Juhan Voolaid
Ranch Hand
Joined: Nov 18, 2003
Posts: 179
|
|
OK ... ive got new problems that i can't find answers. It is with this same example so i continue with this topic. OK after i changed my program a bit it still doesen't work. When i push a button - this action panel will be blank. But to the console in backround appers lot of stuff ... something about: java.lang.nullpointer exception lot of stuff about action performed things and what i think is the most important - EventDispatchThread In this example i didn't use any threads (didn't think it's neccesary) - so i have no idea what should i do next. I think it enaugh if i give u code about 2 classes this buttonPanel and actionPanel - but in my example they are buttonPanel=b and infopanel=i hope u understand and can help me. Here is the code:
|
 |
 |
|
|
subject: i want to call main class constructor from inner class or smt...
|
|
|