views:

183

answers:

6

Hey all, I'm currently working on a Java Swing application and I was looking for some guidence. The application is fairly small, but I'm noticing that as the code base is growing larger, that I have an awful lot of coupling in my object graph. I'm relatively new to Swing, but I've been programming long enough to know where this is headed.

The biggest problem I'm having is setting up my event handling. How should my child windows and objects be communicating events to my higher level objects without having references to them? I've done a fair amount of MVC web coding. Does this pattern lend itself well to Swing? Should I be building my own controller? I guess I'm just fishing for patterns that people have found useful working with Swing.

Thanks in advance for your help.

+2  A: 

Yes. MVC is what you have to use. Here is a very good article about MVC and Swing:

http://java.sun.com/products/jfc/tsc/articles/architecture/

eugener
A: 

As you already said your intent to use MVC , or you may be already using. Once you have seperated out data (I call it as data model layer). Now you need to apply OBSERVER pattern on these data model classes. All the views (your ui components) using this data model are observing this model objects for any change (Via observer pattern).

I hope this is what you are looking for.

YoK
+2  A: 

The best way to reduce coupling in a GUI, I think, is to use an Event Bus. There are several existing implementations out there, including some supporting Swing specifically.

Just google for swing event bus and you'll find.

If you use Guice in your GUI, you may also want to take a look at guts-events.

jfpoilpret
Thanks, this is what I was looking for. An API to provide a messaging bus across the application. I looked at guts-event and http://www.eventbus.org/. I think I'm going to go with the later.
BillMan
That's fine, I have used EventBus in the past and didn't have to complain about it. However, I remember in some versions, using annotations for event consumers could leak memory; I don't know if this has been fixed in latest release.
jfpoilpret
+1  A: 

Another Pattern that might be interesting for you is the MVP (Model View Presenter)-Pattern. This is great for coupling views more loosely to the model. A good explanation by Todd Snyder can be found here.

ymene
A: 

MVC !!! Then you can use also a variant of Observer called Publish/Subscribe in order to implement the event flow inside your app.

Daniel Voina
A: 

I don't agree with the people who suggest to use Event bus, because

  • because of code like EventBus.subscribe(SymbolListChangeEvent.class, this); your whole code will depend on a single event bus instance which makes it very hard to test,
  • it is hard to find out where a specific event is used.

Instead I suggest to use interfaces to encapsulate external dependencies of a module. If you like, you can use them with the listener pattern, but generally are free to refactor everything if you like.

mklhmnn
Event Bus doesn't necessarily mean the bus is a class without any interface, why do you claim so? In the case of guts-events, Event channels are interfaces, that are injected where needed (by Guice) thus easily mockable for testing.
jfpoilpret
For the second point I agree, but you would have the same kind of problem with other approaches. In my experience, I have systematically kept a document (with list of events, meaning, producers and consumers) up-to-date so that maintenance is eased.
jfpoilpret
By the way, what approach do you suggest if you don't like the Event Bus solution?
jfpoilpret
Please search http://www.eventbus.org/confluence/pages/viewpage.action?pageId=819222 forEventBus.subscribe(SymbolListChangeEvent.class, this);IMHO this looks like a single eventbus instance.
mklhmnn
IIRC (but it's a long time I have used EventBus, now I'm using my own exclusively, that one is for sure based on interfaces) EventBus static singleton is just provided as a convenience but you don't really have to use it and can still use EventService interface everywhere you need.
jfpoilpret
Well, then the tutorial/example is crap if it suggests bad code style.
mklhmnn