Java Reference
In-Depth Information
form'swindowingsystem,whichmanagesthe
native screen resource
(anativewindow)
that appears on the screen.
Forexample,whenyouadda
Button
instancetoacontainer,AWTcalls
Compon-
ent
's
void addNotify()
method, which obtains the current toolkit and calls its
ButtonPeer createButton(Button target)
methodtocreatethistoolkit's
Button
peer.
Ultimately,thewindowingsystemisaskedtocreateabuttonnativescreenresource.
For example, on a 32-bit Windows operating system, the native screen resource could
beobtainedviaacalltothe
CreateWindow()
or
CreateWindowEx()
Win32API
function.
AWT components except for those created from nonpredefined classes that directly
extend
Component
or
Container
are known as
heavyweight components
because
oftheircorrespondingpeerinterfacesandnativescreenresources.Componentscreated
fromcustom
Component
and
Container
subclassesareknownas
lightweight com-
ponents
becausetheydonothavepeerinterfacesandnativescreenresources(theyreuse
their closest ancestor's peer, which is how Swing works). You can call
Component
's
boolean isLightweight()
method to determine if a component is lightweight.
Note
Heavyweightandlightweightcomponentscanbemixedinasinglecomponent
hierarchyprovidedthattheentirehierarchyis
valid
(noncontainercomponentsarecor-
rectly sized; container components have their contained components laid out). When
thehierarchyis
invalidated
(e.g.,afterchanging
component bounds
[width,height,and
locationrelativetothecomponent'sparentcontainer],suchaswhenchangingabutton's
text,orafteradding/removingcomponentsto/fromcontainers),AWT
validates
itbyin-
voking
Container
's
void validate()
methodonthetop-mostinvalidcontainer
of the hierarchy.
AsyouexploretheJDKdocumentationforthevariouscomponentclasses,you'lldis-
cover many useful constructors and methods. For example,
Button
declares a
But-
ton(String label)
constructor for initializing a button to the specified
label
text.Alternatively,youcouldcallthe
Button()
constructortocreatea
Button
with
nolabel.Regardlessofwhichconstructoryouuse,youcanalwayscall
Button
's
void
setLabel(String label)
and
String getLabel()
methodstospecifyand
retrieve the label text that is displayed on the button. (Changing a button's displayed
textinvalidatesthebutton;AWTthenperformsvalidation,whichcausesthecomponent
hierarchy to be re-laid out.)
Componentsareeasytocreate,asdemonstratedbythefollowingexample,whichcre-
ates a Yes button: