I created a prototype with two JTabbed Pages:
SourcePage:
Has two JRadioButtons
(Either button generates some factor that modifies the data in the
TargetPage)
TargetPage:
Has several JFormattedTextFields whose value is modified by the
factor passed
by clicking on either JRadioButton in the SourcePage.
Two problems:
Atlhough my model - i mean the data - is working correctly in all
cases, I'm having
the hardest time updating the JFormattedTextFields to match the
changes in the model.
If I click on a JButton, the change in data doesn't show up in the
JFormattedTextFields.
If I change the data in the single editable field in the
TargetPage the remainder of
the JFormattedTextFields dependent on that single editable field
are never
updated on the screen.
Question:
Are there any practical examples I could get hold of? Or could
someone give me some advice
as to what approach I should take at this point. I thought that a
small prototype
i created would help, but having spent a lot of time in vain, I'm
getting 'despondent' :-)
Thanks.
Filip Larsen - 19 Jul 2006 09:56 GMT
<jpshahom@gmail.com> wrote in message
> I created a prototype with two JTabbed Pages:
>
[quoted text clipped - 10 lines]
> match the changes in the model.
> [...]
Without more details it is hard to say exactly what goes wrong. However,
to me it sounds like your code may be listening to event types on the
buttons and/or on the text fields that are not appropriate for the
automatic chained updates you want. In these cases you generally want to
listen for state changes of the underlying model and not user initiated
events like focus change or action events.
When I have to update state and other fields based on radio or check
buttons I use an ItemListener and mostly the logic is such that it
checks if the supplied ItemEvent event has event.getStateChange() ==
ItemEvent.SELECTED before it triggers the update. The DESELECTED events
are useful to disable certain other components, but may of course also
be relevant for your state updates if your update logic requires it.
For the JTextComponents like JFormattedTextFields I'd add a
DocumentListener to the document property of the field (if you have a
lot of DocumentListeners I suggest you make a DocumentAdapter that
forwards the the change types into one method so you dont have to write
out the 3 methods every time). Note that this will trigger an update on
every edit of the field (like for every time the user inserts a
character) so if your updates are expensive you may want to use the
document events to only set a flag and then use that flag from a timer
or similar to only update "when the user aren't typing".
You should also be aware what you may run into the problem of automatic
field updates triggering cyclic changes, like when you code two fields
to automatically change when the other field change, which eventually
can result in different exception types being thrown. Here you may have
to use update flags or compares to break the cycle. And, in order to
detect cyclic problems, remember to always handle exceptions in the
places where you do catch them (ignoring exceptions is a common
programming "design fault" that indeed will make code appear to do
nothing in some cases).
Regards,

Signature
Filip Larsen