Accessing other widgets

0 views
Skip to first unread message

Buzzterrier

unread,
Feb 24, 2008, 11:23:16 PM2/24/08
to Google Web Toolkit
Hello,

I have a strong Ajax/Java background and am having some trouble with
the GWT paradigm. In Ajax/DHTML when I want to manipulate elements on
a page, I simply use javascript (actually the excellent JQuery
library) to access any element in the DOM. Working in GWT I would like
to be able to access a widget from within another widget (so I can
change values, styles etc.). It seems that the best way to do this is
to create static methods in the widgets so they can reference each
other. Is this the best way? Is there some other pattern that people
are using?

gregor

unread,
Feb 25, 2008, 7:29:15 AM2/25/08
to Google Web Toolkit
Hi Buzzterrier,

Probably the most popular pattern to use is the Observer pattern. This
pattern is used extensively in GWT itself and has significant
advantages. The key is do the opposite of what you are suggesting. In
a nutshell, you hide the internal details of your widgets from each
other (e.g. by by extending Composite) and strive to avoid having
direct references between them. You then have widgets broadcast events
to which other widgets can subscribe to as listeners.

This video of a talk at a recent GWT conference by Rajeev Dayal has a
good description of it and how to use it in GWT applications about two
thirds of the way through. I don't think you can go far wrong
following his advice.

http://video.google.com/videoplay?docid=-4141141456426768222&q=Voices+That+Matter+GWT&total=23&start=20&num=10&so=0&type=search&plindex=0

This is the link to a number of other lecture videos from the same
conference which you may find useful.

http://video.google.com/videosearch?q=Voices+That+Matter+GWT



regards
gregor

walden

unread,
Feb 25, 2008, 12:47:22 PM2/25/08
to Google Web Toolkit
Hi Buzz,

Your question is a little general, and there is no single answer,
except that your idea to use static methods won't work.

Gregor's point about Observer pattern should help quite a bit.

Sometimes, it is sufficient if composite A instantiates widgets B and
C, for A to inject mutual references into B and C, knowing that they
will reference each other in many contexts. That may seem rigid in
comparison to Observer pattern, but I find it is the right fit in some
cases where design extensibility is not a concern.

There are other things that help. Certain container widgets maintain
a collection of the widgets that have been added to them, so you have
a sort of poor man's registry available.

These are general answers. If you are having a particular problem
with an OO design involving GWT widgets, post the particulars and I'm
sure you will get some good concrete suggestions.

Walden

On Feb 25, 7:29 am, gregor <greg.power...@btinternet.com> wrote:
> Hi Buzzterrier,
>
> Probably the most popular pattern to use is the Observer pattern. This
> pattern is used extensively in GWT itself and has significant
> advantages. The key is do the opposite of what you are suggesting. In
> a nutshell, you hide the internal details of your widgets from each
> other (e.g. by by extending Composite) and strive to avoid having
> direct references between them. You then have widgets broadcast events
> to which other widgets can subscribe to as listeners.
>
> This video of a talk at a recent GWT conference by Rajeev Dayal has a
> good description of it and how to use it in GWT applications about two
> thirds of the way through. I don't think you can go far wrong
> following his advice.
>
> http://video.google.com/videoplay?docid=-4141141456426768222&q=Voices...
>
> This is the link to a number of other lecture videos from the same
> conference which you may find useful.
>
> http://video.google.com/videosearch?q=Voices+That+Matter+GWT
>
> regards
> gregor
>
> On Feb 25, 4:23 am, Buzzterrier <terry.je...@gmail.com> wrote:
>
>
>
> > Hello,
>
> > I have a strong Ajax/Java background and am having some trouble with
> > the GWT paradigm. In Ajax/DHTML when I want to manipulate elements on
> > a page, I simply use javascript (actually the excellent JQuery
> > library) to access any element in the DOM. Working in GWT I would like
> > to be able to access a widget from within another widget (so I can
> > change values, styles etc.).  It seems that the best way to do this is
> > to create static methods in the widgets so they can reference each
> > other. Is this the best way? Is there some other pattern that people
> > are using?- Hide quoted text -
>
> - Show quoted text -

Buzzterrier

unread,
Feb 25, 2008, 1:07:42 PM2/25/08
to Google Web Toolkit
Hi Walden,

General direction is good, and thanks to you both for your answers. I
am new to gwt and have inherited a code base; sadly it looks like it
is not using best practices so I cannot use it as a learning tool. I
will probably come back with more specific questions down the road,
but your responses let me know what I should not be doing.

Take care!

k-e-n

unread,
Feb 25, 2008, 2:49:58 PM2/25/08
to Google Web Toolkit
GWT allows the usage of a rich event based paradigm.

As one of the other replies suggest, think of your GUI design in terms
of Swing (as this uses a similar event based approach).

In addition you can navigate the widget hierarchies; you can get the
parent of any widget by calling getParent().

Almost all 'widget containers' implement the interface HasWidgets
which amongst other things allows you to iterate through the widgets
in the container.

You can even (if you wish, but I would recommend against it) navigate
through the DOM by using the DOM class in com.google.gwt.user.client.
This same package also contains the class Event which lists all of the
events you can subscribe to.

You can of course simply pass the Java reference from one widget to
another widget (usually when constructing the 2nd widget), however, it
is preferable to avoid this.
If you were to do this it would / should be done in your Java code not
in JavaScript.

You need to think of your application as a Java application where the
GUI happens to be translated into JavaScript & HTML for the purpose of
executing it in a browser, just as server-side Java code is translated
into byte-code so that it can be executed by a JVM.

To understand more about the GWT event system look at the following
interfaces in com.google.gwt.client,ui

Those named Source... and those named ...Listener.

Looking at the interfaces named Has... is also useful so that you can
see how you can interrogate the features / capabilities of widgets
etc. in a Java centric fashion.

In you find that the event based system is to complex or more than you
need at the moment you can still use pointers (references) to widgets,
but rather than having a widget directly bound to another widget, it
is better to have the first widget call a method in a 'controller'
class and to have the controller class modify the 2nd widget. This is
similar to but initially less complex than using events, and is a step
on the way to an MVC (Model, View, Controller) approach.

I hope this helps.
Reply all
Reply to author
Forward
0 new messages