 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Nomak Guest
|
Posted: Fri Sep 09, 2005 2:23 pm Post subject: Synchronizing views of the same data |
|
|
Hello,
let's say that i have a JTextField and a JSlider which enable the user to view the same number in different ways. I can't find a good way to synchronize both of the components.
Right now, i'm using property change events:
- one of the component view/controller is modified by the user
- a change listener is called, and it call the setter
- the setter change the attribute and fire a property change event
- the event i catched by a property change listener wich set the value to the JTextField (which start no event) and set the value of the jslider if it's not the same
the example here is simple to understand, but the real program have more data like that and i have some constraint which are hard to respect because of the synchronization problem:
- view/controller of A is modified
- a change listener call the setter of A
- the setter of A fire a change property event
- the event is catched by another listener
- the listener calculate the constraint and extrat B. Then it set all the view/controller of A, and then the ones of B.
- view/controller of B is modified
- ... almost infinite loop
Have you some pointers (articles, url, pdf) on how to handle this?
I would like to know what already exist before "making the wheel again".
TIA
|
|
| Back to top |
|
 |
Chris Smith Guest
|
Posted: Fri Sep 09, 2005 2:48 pm Post subject: Re: Synchronizing views of the same data |
|
|
Nomak <none (AT) invalid (DOT) domain.fr> wrote:
| Quote: | Hello,
let's say that i have a JTextField and a JSlider which enable the
user to view the same number in different ways. I can't find a
good way to synchronize both of the components.
Right now, i'm using property change events:
|
For anything non-trivial, the way to do this is by sharing a model.
That way, you store the number in only one place and each component
displays the same value.
The JTextField throws a stick in that, though, if it's editable. That's
because you probably want to be a little more lenient about editing than
a direct shared-model would allow. For text fields, you would want to
let the field keep its own model, and update the "master" model at well-
defined times, such as when the field loses focus or fires an
ActionEvent.
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Sat Sep 10, 2005 5:59 am Post subject: Re: Synchronizing views of the same data |
|
|
On Fri, 9 Sep 2005 16:23:27 +0200, Nomak <none (AT) invalid (DOT) domain.fr>
wrote or quoted :
| Quote: | Have you some pointers (articles, url, pdf) on how to handle this?
|
One way is not to use events and Listeners, but when each component
changes it directly modifies the other two, and perhaps triggers an
event for the edification of those outside the inner circle.
Another way to handle it is to use a custom event object with an
additional no-propagate field. You don't even need a field, just a
slightly different type.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
|
|
| Back to top |
|
 |
Per Newgro Guest
|
Posted: Sat Sep 10, 2005 8:39 am Post subject: Re: Synchronizing views of the same data |
|
|
Nomak wrote:
| Quote: | Hello,
let's say that i have a JTextField and a JSlider which enable the user to
view the same number in different ways. I can't find a good way to
synchronize both of the components.
Right now, i'm using property change events:
- one of the component view/controller is modified by the user
- a change listener is called, and it call the setter
- the setter change the attribute and fire a property change event
- the event i catched by a property change listener wich set the value to
the JTextField (which start no event) and set the value of the jslider if
it's not the same
the example here is simple to understand, but the real program have more
data like that and i have some constraint which are hard to respect
because of the synchronization problem:
- view/controller of A is modified
- a change listener call the setter of A
- the setter of A fire a change property event
- the event is catched by another listener
- the listener calculate the constraint and extrat B. Then it set all the
view/controller of A, and then the ones of B. - view/controller of B is
modified - ... almost infinite loop
Have you some pointers (articles, url, pdf) on how to handle this?
I would like to know what already exist before "making the wheel again".
TIA
|
Hi TIA
how about jGoodies (http://jgoodies.dev.java.net)
The binding framework is the point you're looking for. In the tutorial your
problem is solved.
Cheers
Per
|
|
| Back to top |
|
 |
Nomak Guest
|
Posted: Mon Sep 12, 2005 4:08 pm Post subject: Re: Synchronizing views of the same data |
|
|
On Sat, 10 Sep 2005 10:39:34 +0200
Per Newgro:
thx, the pdf http://www.jgoodies.com/articles/binding.pdf is quite interesting but i can't integrate a new library only for one Swing form.
Roedy Green:
way 1: directly modifies the other two : yes but it means writing again a new model which don't trigger, that's what i will do i think
way 2: custom event object: well since the methods which fires and receive the events are part of the JDK, it's a lot of rewritting similar to Sun work (licence problem)
Chris Smith:
yes some kind of master model.
Here is what i have got since the post:
public class MyBoundedRangeModel extends DefaultBoundedRangeModel {
public void setValueQuietly(int n) {
ChangeListener[] changeListeners = getChangeListeners();
for (int i = 0; i < changeListeners.length; i++) {
removeChangeListener(changeListeners[i]);
}
setValue(n);
for (int i = 0; i < changeListeners.length; i++) {
addChangeListener(changeListeners[i]);
}
}
}
This way, i don't propagate:
- i can't start back from BoundedRangeModel interface of else i will copy sun source
- value field is private in BoundedRangeModel, so no access is possible
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|