a class that implements a lightweight component --
for example, a non-AWT class that is a subclass of
Component or
Container
Your class needs to provide
both the look and the feel of the component.
You'll typically need to implement
the following methods:
One or more constructors.
If the component responds to any events,
such as mouse clicks,
the constructor(s) should invoke
the enableEvents method.
The paint method,
so that it draws the component's representation onscreen.
As appropriate, methods to let
the state of the component be changed programmatically --
for example, setText.
Besides changing the component's internal state,
each of these methods should call the
repaint method
if the component's appearance should change.
If the component's size should change,
then call the invalidate method
before calling repaint.
As appropriate, methods to let
listeners be registered and unregistered for the component.
For example, a button
should implement the addActionListener
and removeActionListener methods,
which should add the specified listener using the
AWTEventMulticaster add and
remove methods, respectively.
The contains method
if the component responds to events
to only part of its entire possible drawing area.
Any appropriate processXxx methods.
For example, a button would implement
processMouseEvent
to change the button's state
and to generate actionPerformed messages as appropriate.
Lightweight components place special requirements on
their containers:
Lightweight components sometimes flash noticeably
unless you put them in a container that performs double buffering.
See the
Gauge example
for an example of a double-buffered container.
Lightweight components will not appear
if their container implements a paint
method that doesn't call super.paint.
In other words,
if you implement a container that performs some drawing --
for example, one that draws a box around its display area --
make sure the container's paint method
invokes super.paint!
This section links to four pages,
each with an example
that illustrates an aspect of lightweight components.
Each page includes one applet
and links to the applet's source code.
If you're using a browser that includes support for 1.1beta3,
then your browser will be able to run
the applet on each page.
Otherwise,
you'll need to use another tool,
such as the JDK Applet Viewer,
to view the applet on each page.
Note:
You must use a 1.1 browser such as
HotJava
or the
JDK Applet Viewer
to view the applets on the following pages.
If you have trouble running the applets,
go here.