Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #2525 > unrolled thread

MVC: how do I stop mutual

Started by"Mungo Henning" <mungo.henning@THRWHITE.remove-dii-this>
First post2011-04-27 15:39 +0000
Last post2011-04-27 15:39 +0000
Articles 6 — 5 participants

Back to article view | Back to comp.lang.java.gui


Contents

  MVC: how do I stop mutual "Mungo Henning" <mungo.henning@THRWHITE.remove-dii-this> - 2011-04-27 15:39 +0000
    Re: MVC: how do I stop mu "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:39 +0000
      Re: MVC: how do I stop mu "Mungo Henning" <mungo.henning@THRWHITE.remove-dii-this> - 2011-04-27 15:39 +0000
        Re: MVC: how do I stop mu "ram" <ram@THRWHITE.remove-dii-this> - 2011-04-27 15:39 +0000
    Re: MVC: how do I stop mu "Filip Larsen" <filip.larsen@THRWHITE.remove-dii-this> - 2011-04-27 15:39 +0000
    Re: MVC: how do I stop mu "ralpe" <ralpe@THRWHITE.remove-dii-this> - 2011-04-27 15:39 +0000

#2525 — MVC: how do I stop mutual

From"Mungo Henning" <mungo.henning@THRWHITE.remove-dii-this>
Date2011-04-27 15:39 +0000
SubjectMVC: how do I stop mutual
Message-ID<3vednaqZmPObP2vbnZ2dnUVZ8tqinZ2d@pipex.net>
  To: comp.lang.java.gui
Hi Folks,
Okays, although I'm not the oldest person here I'm struggling to understand 
an issue in MVC
with a Swing java application.
If someone could point me in the right direction I'd be obliged.

Here's the problem: I'm writing a little application and the idea of MVC 
tickles me such that I
want to learn about it and implement it to fully appreciate MVC.
Hypothetically, if I have a plain "String" object in my model and a 
"JTextField" object on my display, I
would software connect these by telling the Model when the end user changes 
the textfield, and using
the Observer pattern tell the JTextField (and any other viewers) when the 
model data changes.

So if the end user types the first character in the JTextField then this 
JTextField nudges the associated
Model and says "data has changed; you'd better update your String".
The model then updates its String object and then it has to tell ALL its 
viewers that its data has changed.
These viewers include the originating JTextField, hence we get a mutual 
loop: A calls B and then B calls A which
leads to A calling B again...

Knowing that others have solved this, how do people break this recursion?
I'd think that whomever informs the Model that the data has changed ought 
not be on the list of Viewers that
the model should subsequently contact.

MVC sounds wonderful; someone enlighten my ignorance please.

Thanks in advance

Mungo :-)

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#2526 — Re: MVC: how do I stop mu

From"Roedy Green" <roedy.green@THRWHITE.remove-dii-this>
Date2011-04-27 15:39 +0000
SubjectRe: MVC: how do I stop mu
Message-ID<uqcdf3l3bt97hg3nladns81ltiurfi8k3i@4ax.com>
In reply to#2525
  To: comp.lang.java.gui
On Sun, 23 Sep 2007 18:29:37 +0100, "Mungo Henning" <mungoh@gmail.com>
wrote, quoted or indirectly quoted someone who said :

>Knowing that others have solved this, how do people break this recursion?

I had a similar problem in JColourChooser.  I simply created a boolean
called "propInProgress".  When it was true, it suppressed propagation
of changes in a spreadsheet-like network of elements.

In a simple case, you can sometimes simply ignore or stop listening to
events at A B when each is triggering unwanted changes in the other.
-- 
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2527 — Re: MVC: how do I stop mu

From"Mungo Henning" <mungo.henning@THRWHITE.remove-dii-this>
Date2011-04-27 15:39 +0000
SubjectRe: MVC: how do I stop mu
Message-ID<KtWdnVU2doZ-JmvbnZ2dneKdnZydnZ2d@pipex.net>
In reply to#2526
  To: comp.lang.java.gui
Hi Roedy,
Obliged for your speedy reply.

"Roedy Green" <see_website@mindprod.com.invalid> wrote in message 
news:uqcdf3l3bt97hg3nladns81ltiurfi8k3i@4ax.com...
> On Sun, 23 Sep 2007 18:29:37 +0100, "Mungo Henning" <mungoh@gmail.com>
> wrote, quoted or indirectly quoted someone who said :
>
>>Knowing that others have solved this, how do people break this recursion?
>
> I had a similar problem in JColourChooser.  I simply created a boolean
> called "propInProgress".  When it was true, it suppressed propagation
> of changes in a spreadsheet-like network of elements.
>
> In a simple case, you can sometimes simply ignore or stop listening to
> events at A B when each is triggering unwanted changes in the other.

Hmmm... but that means that each side of the fence (well, at least one side 
of the fence) has
to have extra code to break the spiral.
So the clean model of "when view changes, alert the model; when model 
changes, alert all views" has to become
a little muddied.
Could there not be a case here to tweak the "observer" capability: when the 
view tells the model that things have
changed the model responds to every other view EXCEPT the triggering view?

Just still trying to find the magic code that takes care of this for me...!

Regards

Mungo :-)

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2528 — Re: MVC: how do I stop mu

From"ram" <ram@THRWHITE.remove-dii-this>
Date2011-04-27 15:39 +0000
SubjectRe: MVC: how do I stop mu
Message-ID<MVC-20070923212708@ram.dialup.fu-berlin.de>
In reply to#2527
  To: comp.lang.java.gui
"Mungo Henning" <mungoh@gmail.com> writes:
>So the clean model of "when view changes, alert the model; when
>model changes, alert all views" has to become a little muddied.

  Java (Swing) does not use MVC.

  In MVC, the view never notifies the model.

  In MVC, the controller notifies the model.

  In MVC, there is no loop, because the change initiated by a
  user action flows from the controller to the model and then to
  the view and then the flow has come to a halt - no loop.

  I am aware of the problem you have posed, 
  but this has to do with Swing - not with MVC.

  For Swing Architecture, see:

http://java.sun.com/products/jfc/tsc/articles/architecture/

  For MVC, see:

http://st-www.cs.uiuc.edu/users/smarch/st-docs/mvc.html

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2529 — Re: MVC: how do I stop mu

From"Filip Larsen" <filip.larsen@THRWHITE.remove-dii-this>
Date2011-04-27 15:39 +0000
SubjectRe: MVC: how do I stop mu
Message-ID<46f6bfe8$0$7604$157c6196@dreader2.cybercity.dk>
In reply to#2525
  To: comp.lang.java.gui
Mungo Henning wrote

> Knowing that others have solved this, how do people break this recursion?


Two possible techniques is to either use a flag in the text field 
listener to indicate an update is in progress and avoid changing the 
text field if this flag is set, or to rely on all the models in use will 
avoid emitting a change event if setting the same value as previously, 
or both.

The first approach requires that you code it yourself, while the second 
is supported as standard by classes that use 
java.beans.PropertyChangeSupport for emitting change events.

If I recall correctly, not many swing models support suppression of 
change events when the new value is "equal" to the old, so there you 
basically has to use the first approach.


Regards,
-- 
Filip Larsen

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2530 — Re: MVC: how do I stop mu

From"ralpe" <ralpe@THRWHITE.remove-dii-this>
Date2011-04-27 15:39 +0000
SubjectRe: MVC: how do I stop mu
Message-ID<1190624919.227585.200610@n39g2000hsh.googlegroups.com>
In reply to#2525
  To: comp.lang.java.gui
I solved this problem by creating my own text field class that derives
from JTextField and overrides the setText method. The overriden method
only invokes super.setText if the new string is different from
getText().

hth
ralph

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web