0

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.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
Bick
  • 17,833
  • 52
  • 146
  • 251

1 Answers1

2

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.

Community
  • 1
  • 1
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • is there any real alternative but JGoodies as far as binding frameworks for java + swing go? – Jens Schauder May 19 '11 at 09:19
  • @Jens Schauder - can't answer that question, I usually use SWT ;) - That's why I gave to pointer to the other question instead of just recommending something – Andreas Dolk May 19 '11 at 09:23
  • Thanks. I usually do the longer writing process just cause it is easier to debug. Frameworks tend to be harder when the there are troubles. I will surely check your recommendation. – Bick May 19 '11 at 09:23
  • @Jens Schauder: there is BBB (Better Beans Binding). Don't you like JGoodies Binding? – jfpoilpret May 19 '11 at 09:37
  • @jfpoilpret I do like JGoodies but I'm always interested in alternatives. I knew about a JSR (295?) which is dead afaik. I'll have a look at BBB. – Jens Schauder May 19 '11 at 10:38
  • Just looked at BBB and it looks just as dead as JSR295 – Jens Schauder May 19 '11 at 10:45
  • Really? Maybe that's due to the changes from java.net (due to Oracle). I don't know. BBB is actually a fork of JSR-295. The main issue I have with it is the lack of documentation (couldn't find any good tutorial to get started). On the other side, my pet peeve with JGoodies is the lack of type-safety, that's why I started rewriting it with a lot of generics (my work can be seen on guts project subversion). JGoodies is very well documented and easy to get started with. – jfpoilpret May 19 '11 at 15:00