Solved

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

Posted on 2011-09-30
7
191 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

840 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