Solved

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

Posted on 2011-09-30
7
188 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
Comment Utility
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
Comment Utility

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
Comment Utility


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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 5

Expert Comment

by:Plk_In_EE
Comment Utility
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
Comment Utility
Yan I will give that a try

Plk I didn't get the link
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
Comment Utility
Thanks works Great!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now