Skip to content
This repository has been archived by the owner on Nov 20, 2017. It is now read-only.
/ swing-mvc Public archive

A simple example of a JavaFX-esque MVC micro-framework for Swing applications

License

Notifications You must be signed in to change notification settings

kasperisager/swing-mvc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Swing MVC

Build Status

A simple example of a JavaFX-esque MVC micro-framework for Swing applications. This was built as a demonstration of how to force some structure onto applications written using Swing. Many of the ideas presented in this micro-framework are based on the JavaFX way of doing MVC.

The framework takes a View-first approach where it becomes the responsibility of a View to initialize its associated Controller and Model. This works well with the window-based structure of Swing applications as Views can then easily be embedded within other Views without having to worry about injecting Controller and Model instances.

Building

The project includes a simple Makefile for compiling and running the project. The following tasks are available:

compile - Compile all Java source files.
run - Run the demo application.
doc - Generate Javadoc from the Java source files.
clean - Clean all output directories.

For the uninitiated, simply run the following from a terminal to execute any given task, substituting [task] with one of the tasks above:

make [task]

Getting started

Generated Javadoc documentation can be found at https://kasperisager.github.io/swing-mvc.

Below are some examples of the different aspects of the framework in use.

Application

The Application class takes care of constructing the main application frame. To create a new MVC application one must therefore extend the Application class and implement the start(JFrame) method:

public final class MyApp extends Application {
  protected void start(final JFrame frame) {
    frame.setTitle("My App");
    frame.getContentPane().add(new MyView(this).render());
  }
}

To get MyApp up and running, simply initialize it in, say, main(String[]):

public static void main(final String[] args) {
  new MyApp();
}

Model

public final class MyModel extends Model {
  private String field;

  public MyModel(final Application application) {
    super(application);
  }

  public void getField() {
    return this.field;
  }

  public void setField(final String value) {
    this.field = value;
    this.emit("changed:field", this.field);
  }
}

View

public final class MyView extends View<MyModel, MyController> {
  public MyView(final Application application) {
    super(application);
    this.model(new MyModel(application));
    this.controller(new MyController(application));
  }

  public JButton render() {
    JButton button = new JButton("This view is a button!");
    button.addActionListener(e -> this.controller().myAction());

    this.model().on("changed:field", (String field) -> {
      System.out.println("Field was changed to " + field);
    });

    return button;
  }
}

Controller

public final class MyController extends Controller<MyModel, MyView> {
  public MyController(final Application application) {
    super(application);
  }

  public void myAction() {
    this.model().setField("Something");
  }
}

Copyright © 2015 Kasper Kronborg Isager. Licensed under the terms of the MIT license.

About

A simple example of a JavaFX-esque MVC micro-framework for Swing applications

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published