Introduction To Applets, Swing & MVC Architecture: Advanced Java Lab
Introduction To Applets, Swing & MVC Architecture: Advanced Java Lab
MVC Architecture
The above figure shows that the top-level Swing classes—the JApplet, JDialog,
JFrame, and JWindow—are direct subclasses of their corresponding AWT
counterparts. The remaining Swing components (Fig. 2, below) are subclasses of
java.awt.Component and java.awt.Container.
Swing V/S AWT
Swing V/S AWT
• It is important to note that the Swing based GUIs are still dependent on
the AWT. Java programs need to have some way to map their windows to
the windowing system used on the native (Windows, Unix, or Macintosh)
platform.
• The AWT’s top-level windows—Window, Frame, Dialog, and Panel—
provide that mapping.
• Also, the JComponent class, which is the basis for all Swing components,
is derived from java.awt.Container.
• Finally, all GUI applications and applets use layout managers (java.-
awt.FlowLayout), fonts (java.awt.Font), colors ( java.awt.Color), and
other non-component classes that are defined in the AWT.
• There is just no way to design a GUI without using AWT classes
The Swing Component Set
Java’s Swing components are defined in a collection of packages named
javax.swing.*.
Swing packages include the following:
javax.swing.event.∗
javax.swing.text.∗
javax.swing.plaf.∗
The javax.swing.event package defines the various Swing events and their
listeners, such as the MenuEvent and the MenuListener. (In the AWT, the
AWT events and listeners were defined in java.awt.event.)
The javax.swing.text package contains the classes for JTextField and
JTextComponent.
The javax.swing.plaf package contains Swing’s look-and-feel classes. The term
plaf is an acronym for pluggable look and feel.
Swing’s platform-independent look and feel is achieved by placing all the
code responsible for drawing a component in a class that is separate from the
component itself. For example, in addition to JButton, the class that defines
the button control, there will be a separate class responsible for drawing the
button on the screen. The drawing class will control the button’s color, shape,
and other characteristics of its appearance.
Model-View-Controller Architecture
Java’s Swing components have been implemented using an
object oriented design known as the model-view-controller
(MVC) model. Any Swing component can be considered in terms
of three independent aspects: what state it’s in (its model), how
it looks (its view), and what it does (its controller).
Model
The model encompasses the state data for each component. For
example, the model of a scrollbar component might contain
information about the current position of its adjustable “thumb,”
its minimum and maximum values, and the thumb’s width
(relative to the range of values). A menu, on the other hand, may
simply contain a list of the menu items the user can select from.
Model-View-Controller Architecture
View
The view refers to how we see the component on the screen. For
example consider an application window on two different GUI
platforms. Almost all window frames will have a titlebar
spanning the top of the window. However, the titlebar may have
a close box on the left side (like the older MacOS platform), or it
may have the close box on the right side (as in the Windows
platform). These are examples of different types of views for the
same window object.
Controller
The controller is the portion of the user interface that dictates
how the component interacts with events. Events come in many
forms - a mouse click, gaining or losing focus, a keyboard event
that triggers a specific menu command, or even a directive to
repaint part of the screen. The controller decides how each
component will react to the event-if it reacts at all.
Model-View-Controller Architecture
Example-1
For example, a button’s role is to appear on the interface waiting to be
clicked. When it is clicked, the button’s appearance changes. It looks
pushed in or it changes color briefly, and then it changes back to its
original (unclicked) appearance. In the MVC model, this aspect of the
button is its view.
Whether a button is enabled or disabled and whether it is pressed or
not are properties of its internal state. Such properties constitute the
button’s model. So we can say that, a button’s view—how it looks—
depends on its model.
Because a button’s state will change when it is clicked or when it is
enabled by the program, some object needs to keep track of these
changes. That part of the component is its controller. Figure-3 shows
how the button’s model, view, and controller interact with each other.
Model-View-Controller Architecture
Figure-3 shows how the button’s model, view, and controller
interact with each other.