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

  • Home
  • SEARCH
  • 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 8198169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:53:40+00:00 2026-06-07T05:53:40+00:00

I use MVC pattern for my project. btnAdd creates new Customer and adds MyJPanel1

  • 0

I use MVC pattern for my project. btnAdd creates new Customer and adds MyJPanel1 as a listener. When Customer changes its state it fires event and customerChanged is called. When
MyJPanel1‘s customerChanged is called it does some action and

  1. it has to stop listening to that Customer
  2. other view/s has to start listen to that Customer

Some of other views will have the same behavior.
Those 2 requirements I implemented in MyJPanel1‘s customerChanged method itself.

UPADTED

My question and problem are:

  1. Is this correct from the point of MVC or I should have some other entity(Controller?) that should handle it somehow?
  2. When Customer fires fireEvent method so c.removeListener(this); in MyJPanel1 removes itself from listeners and by this cause error in fireEvent' becauselistenersinfor` loop was changed.

I’ll appreciate for some help.

class Customer {
    // ...

    private void fireEvent(CustomerEvent event) {
        for (IListener listener : listeners) {
            listener.customerChanged(event);
        }
    }
}

class MyJPanel1 extends JPanel implements IListener {
    MyJPanel2 otherPanel;

    MyJPanel1(MyJPanel2 panel) {
        this.otherPanel = panel;
        createGui();
    }

    private void createGui() {
        JButton btnAdd = new JButton("Add");
        btnAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                Customer c = new Customer();
                c.addListener(this);
            }
        });
        // ...
    }

    @Override
    public void customerChanged(CustomerEvent event) {
        if (event instanceof AddedCustomerEvent) {
            Customer c = event.getCustomer();
            // do something
            c.removeListener(this);
            c.addListener(otherPanel);
        }
    }
}
  • 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-07T05:53:42+00:00Added an answer on June 7, 2026 at 5:53 am

    Views just display data; they never have any functionality on their own.

    Changing things (like adding/removing listeners) is the purpose of the controller. It will hook up the model to the view and make sure that everything works. Make the view as dumb as possible.

    Regarding your second problem: As you noticed, you can’t have listeners that commit suicide. There are several solutions:

    1. Return a boolean from the listener saying “I’m done, remove me”. The loop would then be:

      for (Iterator<IListener> iter = listeners.iterator(); iter.hasNext(); ) {
          if (!listener.customerChanged(event)) {
              iter.remove();
          }
      }
      
    2. Create a new listener that is never removed. Instead let this listener forward the events to the interested views. That means it will send the event to MyJPanel1 until that view is done with it. From now on, the event will go to the other views.

      A good solution would be to have the new listener listen to two events: The CustomerEvent and the IAmDoneEvent published by MyJPanel1.

      It has two lists of listeners. Until IAmDoneEvent is received, all CustomerEvent go to the first list. After this event, all CustomerEvent go to the second list. Add MyJPanel1 to the first list and all other views to the second list.

      Use your controller to build up this listener network.

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

Sidebar

Related Questions

I'm trying to use MVC for a new project after having been around the
I'm trying to use the MVC design pattern for my project but I'm sort
I am trying for several days to find a reason to use MVC pattern
In a swing GUI application, where MVC pattern is applied, how can we use
I currently use the repository pattern in my ASP.NET MVC 3 app. I use
On my web site (asp.net mvc), I use cookie to store search pattern. when
I am using MVC-Viewmodel with repository pattern with EF on my project. I have
I just begin to maintain a Java MVC project which use Java Guice Framework.
I'm attempting to use Ninject to inject repositories into controllers of my MVC project.
I am working on a PHP project which makes extensive use of the MVC

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.