Single Screen Tutorial - Allan-Jacobs/Swing-MVC Wiki

Before starting

This tutorial will walk you through creating an app with Swing-MVC. Setup maven, and add the repo and dependency on the readme before starting this tutorial.

A little about Swing-MVC

Swing-MVC is a lightweight framework for building multi-screen apps, but it can also make single screen apps. It helps by separating of a screen logic into three parts: the Model, the View, and the Controller. This is known as the MVC pattern. At runtime, Swing-MVC searches the classpath for your classes and constructs screens from it.

Registering screens

To register a screen, you need to do two things. The first is to extend the abstract classes and implement the methods for Model, View and Controller. The second is to add the @MVC annotation to each of your classes with the name of the screen they create.


// import necessary classes

// annotate the class with @MVC("EXAMPLE")
// to tell Swing-MVC that this class belongs to the
// "EXAMPLE" screen. This is case sensitive.
public class ExampleModel extends Model {
    // declare, but not initialize, the variables;
    // we are using package-private for our model variables
    // because these components will normally be in
    // a package together
    int count;

    // state is data that can be passed from the previous screen.
    // we can ignore it as we are not using it.
    public void init(Object state) {
        // initialize the data
        count = 0;

// import necessary classes

// annotate the class to tell Swing-MVC that this
// class belongs to the "EXAMPLE" screen
public class ExampleView extends View {
    // declare, but not initialize the variables here
    // these are package-private to allow easier access in
    // the controller.
    JPanel gui;
    JButton incrementButton;
    JButton decrementButton;
    JLabel countLabel;

    public void create(Model model) {
        // due to java's type system, we pass in the model
        // as a `Model` and cast it to the correct type (ExampleModel).
        ExampleModel exampleModel = (ExampleModel) model;

        // we initialize the components here
        gui = new JPanel();
        incrementButton = new JButton("Increment");
        decrementButton = new JButton("Decrement");

        // here we also use data from the model
        // to construct the JLabel with a default value
        count = new JLabel(String.valueOf(exampleModel.count));

        // now we add everything to the JPanel

    // this is used so that Swing-MVC can display
    // the view in the window.
    public JPanel getGui() {
        return gui;

// import necessary classes

import java.awt.event.ActionEvent;

// we annotate the controller with the `@EntryPoint`
// annotation to tell Swing-MVC that this screen is
// the entry point. This annotation can be on any of
// the extended classes (ExampleModel, ExampleView, ExampleController)
// or all of them, but it needs to be on at least one
// we annotate the controller with @MVC("EXAMPLE") to tell
// Swing-MVC that it belongs to the "EXAMPLE" screen.
public class ExampleController extends Controller {
    // these are simply the model and view,
    // but casted already, so you do not need
    // to cast them everytime you use them
    ExampleView exampleView;
    ExampleModel exampleModel;
    // Navagator is a utility class for changing
    // screens (navigating). It also allows you
    // to pass data to the next screen's Model.
    // you may store it in a variable.
    public void init(Navigator nav) {
        // here we create action listeners for the buttons
        // we increment or decrement count, and then update the label
        exampleView.decrementButton.addActionListener(e -> {
        exampleView.incrementButton.addActionListener(e -> {
    // this method is run before navigating to cleanup any resources
    // that the controller is using, such as network connections or
    // unsaved files. since we don't need to cleanup anything,
    // we keep it empty
    public void cleanup() {}

Running the app

The App class is what finds and runs your screens. All you need to do is run the App#createAndStart method in your application's entry point.


// import App class

public class Run {
    public static void main(String[] args) {
        // this runs your app

Compile and run your app. You will see a window popup with 2 buttons and a label, and you can increment or decrement the count.