File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes MVC & Swing - How model classes work together ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "MVC & Swing - How model classes work together ?" Watch "MVC & Swing - How model classes work together ?" New topic
Author

MVC & Swing - How model classes work together ?

Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi guys,

I'd like to hear your opinions about how to make model classes work together in a Swing App.

From my previous experiences with web apps, I know the controller commonly puts all info needed by views as a kind of attribute in session, request or application scopes.

However, I don't know what approach is used with Swing apps.

Reading the Java Tutorial from sun I could understand that in Swing the MVC is more a Model Driven approach than a really MVC pattern as we are used to hear in the web app world.

I also know that in a Swing app, my forms (JFrame, JDialogs ...) may be considered as view, the event listeners as controllers and my custom classes as models, but I'm not confortable about how to stick everything together, do you know what I mean ?

Well, just to simplify my doubt a little bit, let me use the following scenario :

I know it's not a useful real world app but it's just for understanding what I have in mind.

Scenario :
----------
Imagine I have a Swing app where the main window ("Product List Screen") contains a JTable with all products with its respective code, descriptions, unit price, stock quantity and so on ...

On this same window, there's a button ("New Product") which opens a new window ("New Product Screen") for creating the product.

So far, I could identify :

View : The two windows I mention above ("Product List" and "New Product")

Controller : Action Listener used to listen the button click.

Just continuing ...

After open the "New Product" screen, the user is expected to input all product info and them submit the info for insertion.

Here starts my doubt : How to inform the view ("The Product List Screen") of a new product inclusion and ask it to re-read all information from database ?

Even better, how to pass this new product info to the "Previous Product List Screen" as a bean avoiding the need of a read from database ?

If I was using a WEB app, I simply would put such bean as a request attribute and then redirect to the view. But .... What if I'm using a Swing app ? Is there some kind of shared area to put such beans and other model classes ? I don't think so.

I'm really interested about your comments and tips.

TIA,
Edisandro.
[ September 20, 2006: Message edited by: Edisandro Bessa ]

"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
Originally posted by Edisandro Bessa:
How to inform the view ("The Product List Screen") of a new product inclusion and ask it to re-read all information from database?


Usually the Observer Pattern is used in MVC to accomplish this. The model publishes certain types of events like "new product". Anyone wishing to receive events from the model simply "subscribes" to certain events by implementing an Observer interface and registering that with the model. Whenever something in the model changes, the model would notify the registered Observers of the event. So:
  • The "New Product" View/Controller submits the new product to the model.
  • The model obtains the list of observers that registered for "new product" event an notifies each one in turn.
  • The "Product List" View/Controller is one of those registered observers. In response to the "new product" notification it adds the new product as specified to its list.


  • Have a look at EAA-dev: Observer Synchronization and EAA-dev: GUI Architectures.
    Edisandro Bessa
    Ranch Hand

    Joined: Jan 19, 2006
    Posts: 584
    Hum I see.

    Thank you very much Peer for your prompt reply.

    So please, let me ask something more :

    After your explanation I quite understood how to make communication occur between Model and View but my doubt now is :

    Who will create this model ? Product List Screen ? New Product Screen or during the application load ?

    I'm asking this because if I create the model in the New Product Screen, at this time no observer is registered yet. So did you get my point ?

    Thanks a lot.
    Edisandro.
    Peer Reynders
    Bartender

    Joined: Aug 19, 2005
    Posts: 2922
        
        5
    Originally posted by Edisandro Bessa:
    Who will create this model ? Product List Screen ? New Product Screen or during the application load?


    For "classic MVC" the model is established during application load.

    However "Swing MVC" seems to focus on the model of the controls - not the domain model!

    And in the web world "MVC" seems to have been highjacked for what should be a simple matter of "separation of concerns".

    Spend some time reading and understanding Fowler's EAA-dev: GUI Architectures article. MVC has its warts(especially in Swing). There may be better ways towards achieving a separated presentation. Then again your concerns may be addressed with the introduction of a Presentation Model (Application Model).

    See also Fake MVC in Swing - Is real MVC possible?
    Edisandro Bessa
    Ranch Hand

    Joined: Jan 19, 2006
    Posts: 584
    Okay Peer.

    Thanks a lot again for your prompt reply.

    Your posts were very helpful for me.
    Peer Reynders
    Bartender

    Joined: Aug 19, 2005
    Posts: 2922
        
        5
    Now THIS does look interesting
    Presenter First: Organizing Complex GUI Applications for Test-Driven Development (PDF)
    from the Presenter First Page.

    This approach starts out with a variation of the Model-View-Presenter pattern (Martin Fowler has split this pattern into the Supervising Presenter and Passive View pattern) and takes Michael Feathers' The Humble Dialog Box(PDF) approach and evolves it for more complex GUIs.
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    In GUI Architectures Fowler says:

    In this essay I want to explore a number of interesting architectures and describe my interpretation of their most interesting features. ... To some extent you can see this essay as a kind of intellectual history that traces ideas in UI design through multiple architectures over the years.


    It's a good read that may lead you to your own mix of features and ideas.
    [ October 12, 2006: Message edited by: Stan James ]

    A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: MVC & Swing - How model classes work together ?