Solved

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

Posted on 2011-09-30
7
193 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 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

688 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