This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Swing and pattern observer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Swing and pattern observer" Watch "Swing and pattern observer" New topic

Swing and pattern observer

dav mrazek
Ranch Hand

Joined: Aug 11, 2007
Posts: 69
Hello i would like to have opinion on the way i design my application.
The main frame of the application is containing a JList. Via a JDialog i create new Database object that are displayed in the JList as they are created. I update the model of the JList using the pattern observer. The model implements an observer interface and the Database implements subject interface. Each time i create a Database object i update the model with addElement(database). Is this a good way of doing in swing or there are better one ? Thank you.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
It's common to build a Controller class that is separate from the UI and isn't a Swing component at all. When the user updates a value the View (Swing screen or widget) notifies the Controller. The Controller updates the Model (not the little model inside the Swing control, but a Business Model layer in the application). The observable Model publishes a change event, and other observer widgets on the View listen for that.

Google for MVC and make sure you're reading about fat-client MVC, not web style MVC 2. Let us know what questions all that raises.

This gets a little confusing because Swing has "model" objects and actually has a tiny little MVC architecture going on inside its widgets. For this conversation because you mentioned "database" I'm trying to ignore the little MVC inside Swing and think about a larger application design.

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
dav mrazek
Ranch Hand

Joined: Aug 11, 2007
Posts: 69
Alright, i m reading this tutorial which is quite clear, but should i have different controller for different object of the model or different ui component, or just one controller for the whole application ?
dav mrazek
Ranch Hand

Joined: Aug 11, 2007
Posts: 69
Hello, i m really having trouble to understand and implements the mvc pattern in my application. If someone could help me to define the view, the controler and the model and how they can interact each other.
So i have Database object that are created through a popup. When i click on a button after filling details of the database, the Database object is instanciated, the popup is not visible and a JList on the main panel is updated with the new database created. The JList model (extends DefaultListModel) contains the list of database objects.
Thanks for help.
dav mrazek
Ranch Hand

Joined: Aug 11, 2007
Posts: 69
Here is what i did based on different tutorial i have read, but i dont feel that i really implemented mvc.
The controller

Here is the action that displays the popup :

And here is the action that is called when i click on the create button in my popup.

[ December 09, 2007: Message edited by: dav mrazek ]
mandlar suurla
Ranch Hand

Joined: Jun 11, 2008
Posts: 67

I am sorry for upping old thread, but i try to implement also MVC pattern in my Swing app. So i made simple examples which should follow simple rules, like all are independent.


Controller and View. As you can see the controller is inner class which you can make as separate class also you can add sub-controllers, like you got there 5 buttons and all are doing something difficult, you can actually make sub-controllers for that.

One thing what i think should be done a bit differently. View should be class which extends from JPanel and then make the Application creation class where you add view. What you think ?

subject: Swing and pattern observer
Similar Threads
Is this considered MVC?
JList & Observer
wat is MVC architecture ?
GUI application design
MVC v MVVM where to store view data