views:

67

answers:

3

In my current project we are using for our Swing client the following patterns:

Business Object (POJO) <--> (mapping) <--> Presentation Model (POJO with property change support) <--> (binding) <--> view components

Everything is fine and works the way we are expecting them to behave.

BUT, we encouter those problems when view begin to grow:

  1. Many events are fired, resulting in cascading events. One update on a field may result in dozens of subsequent property update
  2. When dialog complexity grows, the number of listeners grows as well, and the code begin to be messy and hard to understand.

Edit after first answer:

  • We don't fire event if there is no change on value.
  • We don't add listener if we have no need for them.

We have screen with very complexes rules and with a need for notification from other related panels. So we have a lots of useful listeners and a single user change can triggers many underlying events.

The idea about binding the presentation model with the business model is not so good for us: we perform some code in the mapping process.


I'm looking for guides, advices, best practices, etc about building maintainable Swing application, especially for the event management side.

+1  A: 
Riduidel
+1  A: 

I suggest a single event action per model. Don't try breaking it down to fields with the hopeless ProtpertyChangeListener. Use ChangeListener or your own equivalent. (Honestly, the event argument is not helpful.) Perhaps change the 'property' type to be a listenable object, rather than listening to the composite object.

Tom Hawtin - tackline
When do you decide to fire the change listener ? If you have a bean with many properties and some are updated in a row how do you decide to fire a single change event instead of many property change events ?
Guillaume
Fire once per operation. If the source is written beans style, then that will be once per `set`. If the source is written in an OO style, then that will be fewer events. Worry about coalescing later (for instance AWT will coalesce repaints).
Tom Hawtin - tackline
A: 

The EventListenerList scheme used by most Swing components is fairly lightweight. Be sure to profile the code before deciding on a new architecture. In addition to the usual choices, another interesting approach to monitoring event traffic is suggested by this EventQueue subclass example.

trashgod