Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Swing and pattern observer

 
dav mrazek
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
dav mrazek
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
Debian Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Model


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 ?

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic