views:

335

answers:

1

I'm trying to create a simple Java desktop application using the Swing Application Framework and the MVC model but I'm struggling on some areas because there is a lack of good examples (the only SAF examples I have found are anything but MVC!).

I manage to fire events from components, but apart from that I struggle to use the MVC model with SAF. Are there any examples anywhere?

For instance, I fire an event (mapped with @Action) in the Viewer which sends it to the controller. But which function should I use? My AbstractController extends PropertyChangeListener.

How do I do the binding with SAF to both directions (model -> controller and view -> controller)?

+2  A: 

I can recommend this article: A Swing Architecture Overview.

In example, when using JTable (view), you create a model by extending AbstractTableModel, and you handle user-events by Actions and listeners.

A user could write some text in a JTextField, and you bind an Action to an "Add"-JButton. Your Action implement actionPerformed() where you can call an Add-method in the model to add the text. In the Add-method you save the data and then calls fireTableRowsInserted(), and the view will be updated.

Swing components contains often a model and a view by default.

EDIT: Sorry, didn't know about Swing Application Framework. My answer was just addressing Swing.

Jonas
So I wouldn't write a separate controller class at all? I'm a newbie with Swing as well which probably explains some of my confusion...
Makis