Java Swing Architecture

  

6 out 10 people working in swing don’t know what’s the foundation architecture of Java Swing. Is it MVC? some kind of Modified MVC? What the heck is the separable model architecture? How is the pluggable look and feel achieved? Read on.. , this page attempts to answer all that in a simple Q and A fashion.

Does Swing have an MVC based architecture?
Not really. While swing has it’s roots on MVC, the swing designers, quickly found that MVC wasn’t the best choice for designing swing.

Why MVC was not suitable for the Swing?
MVC works well when all the 3 parts, the Model , View and Controller can stay independent of each other. Since Swing is mostly UI-oriented technology, it was very difficult for the swing designers to keep the View and the Controller part separate. For eg., it was very difficult in Swing to come up with a soft of generic front controller(as with struts ) that know about every UI component.

So how is Swing Architectured?
Due to the problem stated above, the Swing designers clubbed the view and the controller together, and called this the User Interface(UI). So, now instead of 3 parts as with MVC, Swing has two parts the UI and the Model. So the swing architecture is sometimes called the M-UI architecture.

What’s with the name separable model?
Well, it’s just another name. The M-UI architecture is some times called the separable model architecture because the model is separated from the User interface.

So, in Swing architecture, the controller part is not shared between views, Can the model be shared between different User Interfaces?
Ofcourse! That was one of the basic design goals of swing. For eg., the “ButtonModel” is Shared by JButton,JToggleButton, JCheckBox,JRadioButton, JMenu, JMenuItem, JCheckBoxMenuItem and JRadioButtonMenuItem!


What are the different kinds of model that I can make in Swing?

With swing, you can primarly make two kinds of models,
a) The GUI-State-based models (Shortly, state models ), and
b) The Application-data-based models (Shortly, application models)

It’s unlikely that you would write a State model, but it’s not uncommon. The Button model mentioned in previous questions is an example of State-model

Can you give me an example of State and Application Models ?
The ButtonModel (for eg., stores the state of buttons, checked/unchecked), is an example of state model while a Document Model, used by Textfields, Textarea, etc., to show content to the user are examples of application models

When something changes in the Model how does the UI Know?
The famous Observer pattern is used between the UI and the Model. The UI listens for changes in the model and as soon as there is any change in the model, the model notifies it’s listeners, one of which would be the UI. There are different kinds of notifications. You can find it here.


Who sets up the listener, the model or the UI?

The UI is always responsible for setting up the listener.


How is pluggable look and feel achieved?

oh! that’s a big one. Here is a separate article for that

Hope you found the article good. Add comments to let me know how this can be improved.







3 Responses to 'Java Swing Architecture'

  1. Thierry Lefort - June 18th, 2008 at 7:30 am

    This a really basic vision of the swing architecture, SWING respects the MVC pattern.

    - The Model part is the models no problem with that.

    - The View part is the look and feel, the ComponentUI hierarchy that makes it possible to have multiple look and feel, if the UI and Controller part were mixted as you describe the L&F Swing would mono L&F.

    - The Controller part are the Components they are the link between the UI and the Model.

    Most people don’t write any UI, but it is a really interesting part and should be more used. For exemple a LinkButton is a JButton with a distinct UI presenting it as a Link, this is only a UI change between the regular button and a Link.

  2. Thierry Lefort - June 28th, 2008 at 10:02 am

    Here is a reference to “A Swing Architecture Overview” article from Sun :
    http://java.sun.com/products/jfc/tsc/articles/architecture/

    “One noteworthy point is that as an application developer, you should think of a component’s view/controller responsibilities as being handled by the generic component class (such as. JButton, JTree, and so on). The component class then delegates the look-and-feel-specific aspects of those responsibilities to the UI object that is provided by the currently installed look-and-feel.”

  3. Prom Dresses Emo Fashion Prom Dress - August 17th, 2008 at 10:21 pm

    Prom Dresses Emo Fashion Prom Dress…

    I didn’t agree with you first, but last paragraph makes sense for me…


Leave a Reply





Popular Articles

Blog Categories

Monthly Archives

Resources