Article View: borland.public.jbuilder.java.language
Article #18261Re: How to access to a Grid that is in other class?
From: Paul Furbacher
Date: Thu, 23 Jul 1998 00:00
Date: Thu, 23 Jul 1998 00:00
56 lines
2699 bytes
2699 bytes
Alberto wrote:
> I am a newbie programmer and I have this problem with J[ava].
> When I try to put a value in a GridControl that is in other class but in
> the same package the compiler says that does not find the class, and
> when I give the whole path that I am doing a static reference to a
> non-static variable.
> How can I work with controls that are in other classes?
There are a number of ways to deal with this but the short
solution is that when you want object X to know about object
Y, X has to have a reference to Y. Typically, you do this
either through providing a parameter in the constructor of
X and assigning that value to a member variable, or by providing
an accessor function:
public class X {
private Y y;
public X(Y y) {
this.y = y;
}
// or an accessor function
public void setReferenceToY(Y newY) {
this.y = newY;
}
}
Because you designed class Y and you know that it implements
"setGridControlValue()" method, you can then write (somewhere in
the code of class X):
y.setGridControlValue(newValue);
More elegant solutions revolve around using event messages
and/or intermediary "controller" objects which separate the
"model" from the "views" (frames, dialog boxes, etc.) of your
application. When you use a non-view, or model, object, to
act as the intermediary for handling changes to your application's
views, no view needs to know about any other view. If "frame1"
has some new values, it simply informs the "controller" object
which then determines what to do with those new values. It might
be that the controller pops up a "frame2" with a grid in it, or
who knows. But "frame1" really shouldn't know or care about what
happens. It should just say, "Hey, something has changed, and
here's what changed. If someone is listening, you might want to
do something about it. But if you don't, it won't bother me."
Of course, the whole point of having the controller is that it is
the caretaker, and it figures out what to do.
Paul Furbacher
Technical Details
Message-ID:
<35B78E7C.3F2B7CE8@bilbo.bio.purdue.edu>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<35B5A940.F66E0E5B@jet.es>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,8defb950e00b3e64 X-Google-Attributes: gidf74b3,public From: Paul Furbacher <pf@bilbo.bio.purdue.edu> Subject: Re: How to access to a Grid that is in other class? Date: 1998/07/23 Message-ID: <35B78E7C.3F2B7CE8@bilbo.bio.purdue.edu> X-Deja-AN: 374274798 Content-Transfer-Encoding: 7bit References: <35B5A940.F66E0E5B@jet.es> To: Alberto <alga@jet.es> Content-Type: text/plain; charset=us-ascii Organization: Biological Sciences, Purdue Univ. Mime-Version: 1.0 Reply-To: pf@bilbo.bio.purdue.edu Newsgroups: borland.public.jbuilder.java.language