AWT VS Swing components OR Heavyweight VS LightWeight Components
A heavyweight component is one that is associated with its
own native (provided by OS) screen resource (commonly known as a peer).
A lightweight component is one that "borrows" the screen
resource of an ancestor (which means it has no native resource of its own
-- so it's "lighter"). Lightweight component is painted onto a
heavyweight component. There are some
significant differences between lightweight and heavyweight components.
And, since all AWT components are heavyweight and all Swing components are
lightweight (except for the top-level ones: JWindow, JFrame, JDialog, and
JApplet), these differences become painfully apparent when you start
mixing Swing components with AWT components.
The differences
boil down to the following:
- A
lightweight component can have transparent pixels; a heavyweight is
always opaque.
·
A lightweight component can
appear to be non-rectangular because of its ability to set transparent
areas; a heavyweight can only be rectangular.
- Mouse
events on a lightweight component fall through to its parent; mouse
events on a heavyweight component do not fall through to its parent.
- When
a lightweight component overlaps a heavyweight component,
the heavyweight component is always on top, regardless of the
relative z-order of the two components.
You should not
mix the heavyweight and lightweight components in an application.
Advantages of Swing
1. Swing
components follow Model-View-Controller Architecture/Design Pattern. Model stores the contents and allows them to
modify. View displays the contents in
different forms. Controller handles user
input and interacts with view or model.
GUI components can follow this design pattern because they also have
similar characteristics -
(i)
contents (for example, text in
text field, state of button etc)
(ii)
visual appearance (color, size
etc)
(iii)
behavior(reaction to events)
These 3 characteristics are similar to MVC pattern. For example, a text box can have MVC
pattern. Its model part contains the
text. If a user hits a character key in
a text box, the controller calls “insert” command of the model. The model then tells the view to update
itself. The view never knows why the
text changed. If a user hits a cursor key, then the controller, then controller
may tell the view to scroll. Scrolling
the view had not effect on the underlying text so the model never knows that
this event happened.
2. Wide variety of components
Along with standard components like buttons, check boxes, combo
boxes, text fields, swing has rich set of components for creating trees,
tables, tabbed panes etc.
3. Pluggable look and feel
(Ref. To prev page)
4. Nesting containers
AWT has certain restrictions as far as nesting containers. But swing components provide
flexibility. You can have a graphic in a list, combo box in
toolbox, panels in list box etc.
Disadvantages of Swing
1.
Swing components are slower
than AWT.
Swing components are not thread-safe. You may not get correct output if you modify
the model concurrently with the screen updater thread.
0 comments: