Although the Swing component set provides alternatives to using pre-Swing
AWT components (such as Button, List, and the like), one of the primary design
goals for Swing was that it be based on AWT architecture.
A significant benefit of such layering is that it greatly facilitates
porting AWT-component programs to Swing. In fact, because both the AWT and
Swing component sets use the same AWT infrastructure, it's possible to mix
both kinds of components in the same program -- a technique that allows for
phased migration of applications.
This article will lay out the rules for mixing components, and will provide
some information and guidelines that we hope will make the mixing of AWT and
Swing components as painless as possible.
Heavy and light components
Most of the issues related to mixing AWT and Swing components are related to
the mixing of so-called heavyweight and lightweight components. A
heavyweight component is one that is associated with its own native
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 support was introduced in JDK1.1, and you can read
more about it in the
JDK1.1 Lightweight UI Framework design document.)
We generally don't recommend mixing Swing and AWT components because there
are significant benefits in sticking with programs that are written entirely in
Swing (and thus use only lightweight components).
Some of the benefits of using Swing components are:
- More efficient use of resources: Lightweight components are
really "lighter" than heavyweight components.
- More consistency across platforms because Swing is written
entirely in Java.
- Cleaner look-and-feel integration: You can give a set of
components a matching look-and-feel by implementing them using Swing.
Despite the benefits of using Swing components exclusively, a developer may
sometimes have to mix AWT components and Swing components in the same program
(even when migration is not to blame). For example, such mixing may be required
when a Swing version of a particular AWT component is not yet available.
Because there's sometimes no alternative to mixing heavyweight and
lightweight components, we have provided a few options in Swing to make a
certain level of component-mixing possible. However, as anyone who has tried
this approach knows, there are some practical limitations to this approach.
P.S. (after the article was written info) It will be possible soon to mix AWT
and Swing... How? Read it next Monday