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 3631302
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:22:48+00:00 2026-05-19T00:22:48+00:00

First time posting a question on StackOverflow, so please go easy on me :)

  • 0

First time posting a question on StackOverflow, so please go easy on me 🙂

From what I understand, proper use of the model-view-controller pattern requires that we decouple the view and controller such that the view knows nothing about the controller. I’m having a bit of a problem understanding how to do this using Java Swing.

Say I have a view (some class that would extend JFrame), and this view has a button. Is it safe to say that I would want to register the controller as an ActionListener of the button? Or do I make it a listener of the entire view itself.

And how do I go about doing this without doing something like:

button.addActionListener(myController)

in the view, because if I were to do this in the view code, wouldn’t it now have a dependency on the controller?

I didn’t post any code because, frankly I don’t have much to go on at the moment.

any help is appreicated!

  • 1 1 Answer
  • 1 View
  • 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-05-19T00:22:48+00:00Added an answer on May 19, 2026 at 12:22 am

    It might help to not think of the view in terms of buttons etc. so much as an interface. The interface makes it possible for web ui’s, command line consoles, etc. to be written and fulfill the role of the view.

    In the case of your button event, the button represents a call to some command carried out by the controller.

    So, you could have an interface like this:

    public interface MyViewIf {
        // used by the controller to register its self as a listener of the view
        public addViewListener(ViewListener vl);
        ...
    }
    

    and:

    public interface ViewListenerIf {
        // used by the View to notify any listeners of control events etc.
        public onViewEvent(ViewEvent ve);
    }
    

    Then your controller would implement ViewListenerIf and register it’s self with a factory generated instance of MyViewIf. That way the controller doesnt need to know any specifics about your view class(es).

    Your view class would then internally handle it’s own button events, turn them into ViewEvent objects and call onViewEvent() on the controller that registered it’s self with the view, leaving the View 100% oblivious to the existence of the Controller.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the first time I am posting a question on stackoverflow, so please
First time posting, please let me know if this question has already been answered!
this is my first time posting here, I have a question which I have
first time posting in StackOverflow. :D I need my software to add a couple
That's my first time posting on stackoverflow. I've been finding usefull answers on this
this is my first time posting on StackOverflow but this site is awesome. Thanks
use this website a lot but first time posting. My program creates a number
This is my first time posting to stackoverflow, but these threads have helped me
This is my first time posting a question here, it has been a valuable
this is my first time ever posting a question and I am kind of

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.