Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How Do I Access an Object in the Main Code from another Object?

Posted on 2011-09-30
7
Medium Priority
?
197 Views
Last Modified: 2012-05-12
Main code
   object is selected

then a:
dialog panel
scroll panel
plain panel are created to modify the selected object.

How do I do gets and sets on the selected object from the panel?

In the end, when I hit the save button on the panel, I have to tell the main code to save the object. I can see how I could pass the code up the chain of objects, but how to I pass the changes to the main code object and tell the main code to save it?



0
Comment
Question by:alukes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36895605
When you create child objects from original object you pass to these children
the handle back to the original object and then you can use this handle in the child object to
invoke methods on the original object and in this way you'll be able to change your original object
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36895611

Look at this code, and let me know if there is something not understandable:


public class OriginalObject {

public OriginalObject() {

// do something inside constructor or call other methods of OriginalObject

//then somewhere you say:

ChildObject co = new ChildObject(this, a, b, c...);




}

public void methodOfOriginal( int a, String b, int x, float y){


}




}



//   this is the beginning of ChildObject

class ChildObject  {

OrigonalObject oo;

int a;

String b;

float c;

ChildObject (OriginalObject oo, int a, String b, float c ) {

this.oo = 00;
this.a = a;
this.b = b;
this.c = c;



} 


SomeMethod() {

//doing something and then what to pass some data and behaviour back to original object

oo.metodOfOriginal(a,b, x, y);



}




}

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36895616


corrected some misprints:

public class OriginalObject {

public OriginalObject() {

// do something inside constructor or call other methods of OriginalObject

//then somewhere you say:

ChildObject co = new ChildObject(this, a, b, c...);




}

public void methodOfOriginal( int a, String b, int x, float y){


}




}



//   this is the beginning of ChildObject

class ChildObject  {

OrigonalObject oo;

int a;

String b;

float c;

ChildObject (OriginalObject oo, int a, String b, float c ) {

this.oo = oo;
this.a = a;
this.b = b;
this.c = c;



} 


SomeMethod() {

//doing something and then when you want to pass some data and behaviour back to original object you invoke method on the
//instance of original object

oo.metodOfOriginal(a,b, x, y);



}




}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:Plk_In_EE
ID: 36895985
Hi There,
Please refer the below portal link for your reference.

accessing objects in a different way explained here.

Thanks
karuppan
0
 

Author Comment

by:alukes
ID: 36896739
Yan I will give that a try

Plk I didn't get the link
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36897120
Yes, give it a try - it should work - as this is the very basic way of communicating between the classes in Java proigram - this very often how you communicate betwenn the windows in your GUIu application where the frame (extending JFrame) will be your OriginalObject and the diallog window extending JDialog will be your child window.

In many cases you may also wanbt to make variable "co" from the code aboive to be not local (as shown in my code) but rather an instance variable, so that the child window and its methods would be accessible from aany method of the OriginalObject. Witth that you'll establish a two-way connection beytween objects - so user changed something on a child - child executed some method on the parent - parent then made some vcalculation, based on the caLculation original oibject made some decision and reported it back to child, which through some change in its GUI elements communicated the decision of the oiriginalobject back to user - this is one of the very common scenarios.
0
 

Author Comment

by:alukes
ID: 36897411
Thanks works Great!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question