title: Apply the MVC pattern to an existing application
body: <p>This is a follow up question to <a href="https://stackoverflow.com/questions/5217611/the-mvc-pattern-and-swing">The MVC pattern and SWING</a>. I am trying to implement the MVC pattern in an existing piece of code. The existing code creates a JFrame which acts as both the view and the controller. The JFrame has a table, and the table's model is an adapter to a custom data model. Whenever the user performs an action, the model would be updated by doing something like the following:</p>
  
  <pre><code>CustomDataTableModel cdtm = (CustomDataTableModel) DataTable.getModel();
  CustomDataModel cdm = cdtm.getModel();
  cdm.delete(1);
  </code></pre>
  
  <p>I've tried to visualise how it currently works, but I've also visualised how I imagine the relationships with the future controller and model should be.</p>
  
  <p><img src="https://i.stack.imgur.com/sDbV8.jpg" alt="MVC"></p>
  
  <p>Now, my question is simply whether I can continue using the model as it is now? Could I implement the following and still "adhere" to the MVC pattern?</p>
  
  <ol>
  <li>The user selects an element in the table, and clicks on a <strong>delete</strong> button.</li>
  <li>The view delegates the action to the controller.</li>
  <li>The controller access the table through an accessor on the view, and performs the update.</li>
  <li>The model, when it is updated, notifies the JTable that it has been changed. </li>
  </ol>
  
  <p>If any other components in the view displays data from the table, then this could be solved by registering listeners on the JTable's table model.</p>
  
  <h3>Update 1</h3>
  
  <p>I've been thinking about the existing code in light of the MVC pattern, and I've redrawn the relationships a little. The point is that the controller is the behaviour of the view, thus the controller updates the model when users do stuff, and the view listens for changes in the model. However, nothing in the MVC pattern stops the view from listening to the model through a tablemodel - right?</p>
  
  <p><img src="https://i.stack.imgur.com/rxDXH.jpg" alt="MVC"></p>
  
  <p>Now, the user clicks the add button. The view notifies the controller that the add button has been clicked, and the controller takes care of creating a new item by invoking some method on the model. The view is registered as a listener on the model (through a table model) and updates its view. The controller may also be a listener on the model in case it needs to take care of disabling or locking fields. Have I not achieved what the MVC is all about; separation of concerns? As far as I can see, I've even introduced the adapter pattern to decouple the view even more from the model? It is late and I am tired, so that might be why it makes sense :-)</p>