Solved

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

Posted on 2011-09-30
7
192 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
  • 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 500 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

733 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