I'm trying to make a simple drawing application with the MVP pattern. Let's consider I want to be able to draw a circle and drag (move) the circle around on a JPanel. After the drag, the model should be updated. Further, in a second panel the center position (x,y) of the circle should be updated as well. Altough it seems to be overkill, I'd like to implement the second panel also as MVP.

Tasks to be performed:

  • Drag a circle with the mouse
  • The position of the circle should be stored in the model
  • The view should only draw, what's in the model ( a circle at a specific position)
  • The second MPV view should show the value of the x and y position of the circle in text form.
  • At some point the user can click a save button, which will take the models data and stores it to a text file. On startup the position from the text file will be written to the model and the view refreshed.

Now here are my questions:

  1. I've read a lot of different solutions on how to implement the MVP pattern in general an specific with jgoodies. jgoodies seems to be perfect for standard gui components like text fields. As far as I understood, the viewer should only be passive. Thus the viewer visualizes the values hold in the model. Now, should the viewer call the presenter in a onDrag() method or should the peresenter call a viewer.getDragPane().addListener() method?
  2. I assume in either implementation, the value in the model will be set. Who is telling the view to refresh, based on the new values in the model? With jgoodies the view is updated automatically, but how to do it without jgoodies? Should I have a method in the viewer class like, viewer.setData(model). I'm asking because I've read, that the view should know nothing about the model.

  3. Now the second MVP (table with centerX, centerY as label) needs to be notified by someone. Who should this be? I've read that messagebusses are commonly used, but from my personal experience it's a bit "uncool" to debug messagebusses. Is there a best practice?

  • 1
    This is really vague, and only focuses on patterns. What about the requirements of your actual application? The only thing you've said about that is that it is a drawing application. – Robert Harvey Jul 31 '18 at 0:23
  • I did edit my qustion and added the tasks.. – blaster Jul 31 '18 at 5:07
  • 2
    Worth clarifying that MVP is Model–view–presenter at first mention? When I read it I was initially thinking Minimum Viable Product. – bdsl Jul 31 '18 at 6:21
  • also see this similar question : softwareengineering.stackexchange.com/questions/316931/… – Ewan Aug 1 '18 at 10:26

I'm not familiar with jgoodies but I think you have two standard approaches to your event handling problem

  1. Have a parent view and presenter for the entire window including both panels.

    This will allow you to communicate the drag event up from one panel and push the model updates to another.

  2. Use the mediator pattern

    Have a message bus as you say, which you inject into each presenter having them listen and handel events. This allows disconnected panels to communicate via the bus.

    It's a recognised pattern and useful, but it can add some loose coupling in the form of the names or types of events which you have to know in order to listen for them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.