Skip to main content

MVnC architecture for Swing GUIs

Posted by evanx on June 19, 2008 at 6:52 AM PDT

Rather than put our "application logic" in a "messy" view class, we create a separate "controller" class, with event handlers. We wish this controller class to be as neat and tidy as possible e.g. with minimal boilerplate or much else besides our application logic.

Let's explore how we might achieve this, using an annotation-driven application framework, with some AOP and convention-over-configuration.

Gooey MVnC: A part of "Gooey Beans, a trilogy in 42 parts"


Gooey MVnC Guidelines Cheatsheet
View  Controller  Model beans 
Code Messy Neat Trivial
Tool GUI Designer
Rules No logic No strings
Automation  Beans Binding Events & Tasks  Validation
Tricks Resource injection  AOP

Code Snippet

Our controller class includes annotations in order to enable our framework infrastructure to automatically map events from our GUI components to event handler methods, and to support background tasks and EDT-switching via AOP.

public class LoginController extends GBasicController {
   @View LoginView view = new LoginView(); // JPanel with components
   @FormBean User user = new User(); // for form fields auto binding
   @BackgroundTask void fetchUsers() throws DatabaseException {
      ... // long running task

   @BackgroundTaskDone void fetchUsersDone() {
      ... // update GUI with results from task

   @EventHandler void okActionPerformed() {
      ... // handle OK button pressed
   @EventHander void usernameEntered() throw DataException {
      ... // validate username exists in database

Annotations in our model beans, eg. User, relate to binding and validation, where we wish to support automatic binding of view components to bean properties, eg. by matching component names to bean property names.

Resources - where i will collate these articles and their code.

Related Topics >>