Bick
Bick

Reputation: 18521

Java swing - how to bind my fields to a module

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 :

  1. Create the gui elements.
  2. On the module create a list that holds all the listeners to the change in the specific field.
  3. On the gui - at the beginnig add a listner to the module [send an anonymous class (value changed)]
  4. On the Module - in the setter - iterate the listeners list and notify all.
  5. 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.

Upvotes: 0

Views: 896

Answers (1)

Andreas Dolk
Andreas Dolk

Reputation: 114777

Best practise to me is to choose a databinding framework and use it consequently. Databinding is exactly what you want - a synchronization between model (sic!) and UI

Here's a Q/A page on SO that includes a lot of links to databinding frameworks for Java Swing applications.

Upvotes: 2

Related Questions