views:

669

answers:

3

What's the easiest way to build an applet that has a view with components that are bound to model data and update when the model is updated?

Ideally as little code as possible, preferable none/declarative :)

If a component type is needed for explanation, please consider a JLabel whose text is bound to a bean with a String getText() accessor - but if that's just plain dumb, please give me a better example!

Thanks!

+1  A: 

The first thing that comes to mind would be to register a PropertyChangeListener on the bean which updates the label's text in its propertyChange method. That's the usual way of linking models and views, add a listener to the model which updates the view on any relevant changes.

David Zaslavsky
+1  A: 

unless your model is very small, data binding is not so easy:

http://www.jgoodies.com/ examples: http://www.java2s.com/Code/Java/Swing-Components/Data-Binding.htmjgoodies

BeansBinding: http://www.artima.com/forums/flat.jsp?forum=276&thread=213997

Ray Tayek
+1  A: 

I suggest avoiding PropertyChangeEvents and anything beanish.

Make fine grained models: for instance, a model representing a piece of text (Document is difficult to use and heavyweight, but you can use adapters). You will also need to be able to model constraints (integer bounds for example) and derived models. Then your "real" "business" are can be composites, with no setters or event handling. Avoid duplicating data in the model.

With simple models available, wiring to components is easy. For instance to create a label wired to a text model, have a factory method that takes the text model and returns a new wired-up JLabel.

Tom Hawtin - tackline