Here is a kickstart guide to Swing MVC architecture. Swing isn't a pure implementation of MVC, rather it follows a modified version of MVC.
Swing supports MVC architecture (Model-View-Controller) based
architecture for its components. But what does MVC mean? Well, let me
explain a bit about it.
Please go through Introduction to Swing, if you haven't gone through.
Model - It is the data of the component. For example, if we take a Button, the text in it is a model.
View - The look (a.k.a. the visual appearance) of the component.
Controller - The controller sends commands to update the model and/or the view.
Let us consider a practical example, take a Text field. It is a GUI component that is visible to your eye. When you create and add it to a frame and present it to the user, the user is able to see it and write text in it.
The user is able to see it - So it is a view. He is able to see the white background, border, text in it.
The user is able to update the text in it - So it is a model. Now, don't consider the appearance, only the text, the data. When you call getText() after the user has typed some text in it, you get what he has typed. So, model represents the data in the component.
When the user types, he is able to update the text in it (the model) as well as the visual appearance (the view -- whatever he types is displayed in the text field) - So it has a controller that is responsible for the model and the view being updated.
Well, now you have learned what do they mean? Now, what is MVC Architecture in total, all these three are separated to maintain cleaner code. The advantage of this is that you can plugin the look and feel you want without having any side effects on the GUI component, either on its data or the way it functions.
Does swing strictly follow the MVC?
No. It uses a modified version of the MVC where the View and the controller are coded together called as UI delegate. So it is called Model-Delegate architecture or Separable Model Archicture. So when you are asked, say the same thing. Both of these are one and the same and they are based on MVC but are not its classical implementation.
Every swing component has a model and it is an interface. There are
implementations of it, you don't need to worry about them. You can also
re-write the model of a swing component by implementing these
interfaces, but at last you need to remember view and controller,
together called as UI Delegate and your programs doesn't directly
interact with the delegate.
For JButton, ButtonModel is an interface and it is the model and ButtonUI is the UI delegate.
No comments:
Post a Comment