Note that there are some explanatory texts on larger screens.

plurals
  1. POThe Elegant way to handle Cyclic Event in Java?
    primarykey
    data
    text
    <p>i think this not a specific problem to me; everybody might have encountered this issue before. To properly illustrate it, here's a simple UI:</p> <p><a href="http://www.freeimagehosting.net/uploads/7aec06ea46.png" rel="nofollow noreferrer">alt text http://www.freeimagehosting.net/uploads/7aec06ea46.png</a></p> <p>As you can see, those two spinners are controlling a single variable -- "A". The only difference is that they control it using different views.</p> <p>Since these two spinners' displaying values are synchronized, cyclic event shows up.</p> <p>If i change the top spinner, "A" will be changed and the bottom spinner's value will also be updated accordingly. <strong>However</strong>, updating the bottom spinner's call (such as setValue) will also trigger another event instructing the top spinner to update based on the bottom spinner's value. Thus creates a bad cycle which can eventually cause a StackOverFlow exception.</p> <p><em>My previously solution is kinda cumbersome: i placed a guarding boolean to indicate whether the 2nd updating call should be performed.</em></p> <p>Now i'd like to ask "<strong>how can i handle such situation elegantly?</strong> ( <strong>in general, not specific to spinners</strong> )"</p> <p><em>thx</em></p> <hr> <h2>Update:</h2> <p>Since i've got 2 answers suggesting me to utilize the observer structure, i have to say something about it.</p> <p>Like what i've said, it's great but far from being perfect. Not only because of its inherent complexity, but also <strong>Its inability to solve the problem</strong>.</p> <p>Why? To see the reason, you must realize the <em>tight coupling</em> of the View and Model-Controller in Java Swing. Lets take my spinner UI for an example. Suppose the variable A is actually an Observer object. Then, after firing the first state change event from the top spinner, the Observer "A" will update its value and fire a PropertyChange event to notify the bottom spinner. Then comes the 2nd updating which updates the bottom spinner's View. <em>However</em>, changing bottom spinner's view inevitably triggers a redundant event that will try to set "A"'s value again. Afterwards, the deadly loop is fully constructed and the stack overflow will be thrown.</p> <p>In theory, the Observer model tries to solve the direct cycle by introducing 2 independent feedback paths. The chained updating odds(in event-response codes) implicitly form a bridge connecting both paths, making a cycle again.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload