🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #172

Re: What is this?

#172
From: John McGrath [TeamB]
Date: Thu, 22 Jul 2004 06:41
81 lines
5123 bytes
On 7/21/2004 at 4:18:27 PM, Ernesto Basc�n wrote:

> By example, I have my own "color chooser dialog"; I want the red, green,
> blue text fields to show their respective values when the user clicks a
> color selection panel, and I want the color selection panel selector to
> move on automatically everytime the text fields value change.
> 
> When the user clicks the color selection panel, an event tries to modify
> the text field values and that fact raises an event trying to modify the
> color selection panel that raises again the event to modify the text
> field values and so on.....

Got it.  There are two approaches  to do this sort of thing.

Common Model
------------

Swing components use a Model-View-Controller (MVC) architecture.  Ignoring
the controller part, which in Swing is kind of mixed in with the view, you
have two components:  The model, which holds a value, and the view, which
shows the value.

Swing UI (view) components will automatically create an appropriate model,
so it is easy to use them without worrying much about the model, but it is
always there.  When you get to the more complex components, being aware of
the separate model and view becomes more and more important.

In your case, you have 4 different view components that all show the same
value (or one aspect of that value).  If you write your code with a single
model component, then everything will be updated automatically and you
will will not have to listen to events and update the other components.

I do not know what your color selection panel looks like, so it is hard to
say exactly how you would implement this.  My guess is that the simplest
approach would be to create a model component composed of 3
PlainDocuments.  These documents would serve as the model for the text
fields.  This model component would listen to the documents and update the
color value when one of them changed.  When the color value actually
changed, it would fire a stateChanged() event.  The color selection panel
would listen for these stateChanged() events and update itself when that
occurred.

External Listeners
------------------

The second approach is what you have tried.  This is definitely not as
clean as the above approach, but it is a workable solution.  And in some
cases, the common model approach cannot be used because you cannot change
the model.

The biggest trap with using external listeners is the one you have fallen
into - recursive event firing.  The critical thing is that you only change
the value of one component when the value of the other component actually
changes.

If you do that, the change happens like this:  Both components start out
with the same value, say 1.  The user changes the value of component A to
2.  Component A notifies your listener that the value of A has changed,
and the listener sets the value of component B to 2.  As a result of this
change, Component B notifies your listener that its value has changed, so
that listener sets the value of component A to 2.  However, its value was
already 2, so no change has occurred and the component does not fire an
event.  This chokes off the recursive event firing.

Two of the possible pitfalls with this approach are temporary change
events and value rounding.  The first occurs when a component ends up
changing and then immediately changing back.  Since the component's state
*does* change, the recursion does not get choked off.  The rounding
pitfall occurs when a text field is used to display a value and then the
value is computed from the text field's string value.  In this case, the
value actually changes, even though it does not appear to be, since the
text display remains the same.  So again, the recursion is not choked off.

-- 
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
    http://www.JPMcGrath.net/newsgroups/e-mail.html
Technical Details
Message-ID: <40ff6193$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!news-feed01.roc.ny.frontiernet.net!nntp.frontiernet.net!newshosting.com!nx02.iad01.newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <40a0c784$1@newsgroups.borland.com> <40a0ca1b$1@newsgroups.borland.com> <40fc4c4c@newsgroups.borland.com> <40fd28e5$1@newsgroups.borland.com> <40fd399e$1@newsgroups.borland.com> <40fdf2d8@newsgroups.borland.com> <40fece0c$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 1089d0,db80065efbaa27c8
X-Google-Attributes: gid1089d0,public
X-Google-Language: ENGLISH,ASCII
From: "John McGrath  [TeamB]" <ng-b@JPMcGrath.net>
Subject: Re: What is this?
Newsgroups: borland.public.jbuilder.java.api
References: <40a0c784$1@newsgroups.borland.com> <40a0ca1b$1@newsgroups.borland.com> <40fc4c4c@newsgroups.borland.com> <40fd28e5$1@newsgroups.borland.com> <40fd399e$1@newsgroups.borland.com> <40fdf2d8@newsgroups.borland.com> <40fece0c$1@newsgroups.borland.com>
User-Agent: XanaNews/1.16.3.1
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15
Content-Transfer-Encoding: 8bit
NNTP-Posting-Host: 151.197.205.61
Message-ID: <40ff6193$1@newsgroups.borland.com>
Date: 21 Jul 2004 23:41:23 -0700
X-Trace: newsgroups.borland.com 1090478483 151.197.205.61 (21 Jul 2004 23:41:23 -0700)
Lines: 81
X-Authenticated-User: jpmcgrath
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!news-feed01.roc.ny.frontiernet.net!nntp.frontiernet.net!newshosting.com!nx02.iad01.newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail