Small Open Source Java Tools and Libraries

Simple MVC example

This example shows you how to prevent UI methods from being called outside the Swing EDT (Event Dispatching Thread) and also how to decouple the controller from the view without using a SwingWorker External link. WARNING: If the controller has instance variables, you will have to access them in a thread-safe way!

View source:
package org.fuin.jaddr;

import javax.swing.JPanel;
import javax.swing.JTextField;

import org.fuin.aspects4swing.InvokeAndWait;

public class MyView extends JPanel {

    private MyController ctrl;

    // ...

    public void setAddress(final Address address) {
        // If the calling code isn't already running inside the EDT (Event
        // Dispatching Thread) the thread will be switched using
        // "SwingUtilities.invokeAndWait(..)" before calling this method.

        // ... Set data in UI controls ...

    private void buttonLoadPressed() {
        final String idStr = getTextFieldId().getText();
        final Integer id = Integer.parseInt(idStr);


Controller source:
package org.fuin.jaddr;

import org.fuin.aspects4swing.StartNewThread;

public class MyController {

    private MyView view;

    private Address address;

    // ...

    public void loadAddress(final Integer id) {
        // A new thread will always be started before calling this method

        // Load address from database
        // ...

        // Set data in view



Usage with Maven (Configuration)

Maven Dependency:

AspectJ Maven Plugin: