I am writing in java swing and I am wondering if there is a better(shorter) way to bind my object to the gui forms.
I will describe this need for an example - Zero Button :
Lets say I want on the GUI to have a number field and a button that puts zero in it. That's it.
I have a gui - form and java class . and a module class that holds all the data and does some logic.
What I will usually do (and I think its awful) is :
- Create the gui elements.
- On the module create a list that holds all the listeners to the change in the specific field.
- On the gui - at the beginnig add a listner to the module [send an anonymous class (value changed)]
- On the Module - in the setter - iterate the listeners list and notify all.
- On the gui - my listener changes the TextField to zero.
This whole process just to put a zero in the field next to it :-0).
and now for each field...
This is waaaay to long for me .
(Sometimes i just want to set the module to zero and than set the gui text to zero in two simple lines of code)
Is this the best practice?
Thanks.