Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7751049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:26:56+00:00 2026-06-01T11:26:56+00:00

In a swing GUI application, where MVC pattern is applied, how can we use

  • 0

In a swing GUI application, where MVC pattern is applied, how can we use Spring to wire the model view and controller? i.e. what beans (model, view or controller) should be injected using spring and what should be created from the application? I have applied the MVC pattern described here when developing the application. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:26:57+00:00Added an answer on June 1, 2026 at 11:26 am

    I defined all the beans in spring and used a factory method to create the views when required. Controller is injected to the view and the model and view are added to the controller via spring.

    Following are the code samples from a simple example that I came up with, in order to find a solution: (sorry for the long post!)

    the application context file:

    <bean id="firstModel" class="com.model.FirstModel"></bean>
    <bean id="secondModel" class="com.model.SecondModel"></bean>
    
    <bean id="firstController" class="com.controller.FirstController" />
    <bean
        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject">
            <ref local="firstController" />
        </property>
        <property name="targetMethod">
            <value>addModel</value>
        </property>
        <property name="arguments">
            <list>
                <value>FIRST</value>
                <ref local="firstModel" />
            </list>
        </property>
    </bean>
    <bean id="secondController" class="com.controller.SecondController" />
    <bean
        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject">
            <ref local="secondController" />
        </property>
        <property name="targetMethod">
            <value>addModel</value>
        </property>
        <property name="arguments">
            <list>
                <value>SECOND</value>
                <ref local="secondModel" />
            </list>
        </property>
    </bean>
    <bean
        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject">
            <ref local="secondController" />
        </property>
        <property name="targetMethod">
            <value>addModel</value>
        </property>
        <property name="arguments">
            <list>
                <value>FIRST</value>
                <ref local="firstModel" />
            </list>
        </property>
    </bean>
    <bean id="firstForm" class="com.view.FirstForm">
        <property name="controller">
            <ref bean="firstController" />
        </property>
    </bean>
    <bean id="secondForm" class="com.view.SecondForm">
        <property name="controller">
            <ref bean="secondController" />
        </property>
    </bean>
    

    following is the abstract controller class:

    public class AbstractController implements PropertyChangeListener {
    
    Map<Type, BaseView> registeredViews;
    Map<Type, AbstractModel> registeredModels;
    
    public AbstractController() {
        registeredViews = new HashMap<Type, BaseView>();
        registeredModels = new HashMap<Type, AbstractModel>();
    }
    
    public void addModel(Type type, AbstractModel model) {
        registeredModels.put(type, model);
        model.addPropertyChangeListener(this);
    }
    
    public void removeModel(AbstractModel model) {
        registeredModels.remove(model);
        model.removePropertyChangeListener(this);
    }
    
    public void addView(BaseView view, Type type) {
        registeredViews.put(type, view);
    }
    
    public void removeView(javax.swing.JFrame view) {
        registeredViews.remove(view);
    }
    
    public void propertyChange(PropertyChangeEvent evt) {
    
        for (BaseView view : registeredViews.values()) {
            view.modelPropertyChange(evt);
        }
    }
    
    protected void setModelProperty(String propertyName, Object newValue) {
        for (AbstractModel model : registeredModels.values()) {
            Statement statment = new Statement(model, "set" + propertyName, new Object[] { newValue });
            try {
                statment.execute();
            } catch (NoSuchMethodException e) {
                continue;
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    }    
    }
    

    following is the abstract model class:

    public class AbstractModel {
    
    protected PropertyChangeSupport propertyChangeSupport;
    
    public AbstractModel() {
        propertyChangeSupport = new PropertyChangeSupport(this);
    }
    
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }
    
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }
    
    protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
        propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
    }    
    }
    

    Following is the code sample of the view interface:

    public interface BaseView {
    
    void modelPropertyChange(PropertyChangeEvent evt);
    
    public abstract void showForm();
    
    }
    

    following is the code sample of the factory class:

    public class FormFactory {
    
    private ApplicationContext context;
    private static FormFactory viewFactory;
    
    private FormFactory() {
        if (context == null) {
            context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        }
    }
    
    public static synchronized FormFactory getInstance() {
        if (viewFactory == null) {
            viewFactory = new FormFactory();
        }
        return viewFactory;
    
    }
    
    public BaseView createForm(Type type) {
        BaseView form = null;
        switch (type) {
            case FIRST:
                form = (BaseView) context.getBean("firstForm");
                break;
            case SECOND:
                form  = (BaseView) context.getBean("secondForm");
                break;
            default:
                break;
        }
        return form;
    
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to create a new Swing GUI desktop application template using NetBean 7.0
Greetings everyone! I programmed GUI Application using Java Swing under Windows. Under windows everything
I'm writing a Java application with GUI using Swing. One of the GUI components
I'm developing Swing application, and everything works fine usually. But I have an GUI
I am creating one GUI in swing Java. I have to use one button
I am writing a Desktop GUI application in Clojure using Java Swing. Normally when
I'm building a Java application with a Swing GUI and a task that takes
I'm using proxy to extend various Swing classes in a Clojure GUI application, generally
I have created a Swing Application- GUI containing fields like TextFields, Labels, CheckBoxes and
In my Java application with a Swing GUI, I would like to achieve the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.