Using Components, the GUI Building Blocks |
One or more component events are generated by aComponent
object just after the component is hidden, made visible, moved, or resized. An example of a component listener might be in a GUI builder tool that's displaying information about the size of the currently selected component, and that needs to know when the component's size changes. You shouldn't need to use component events to manage basic layout and rendering.The component hidden and component visible events occur only as the result of calls to a
Component
'ssetVisible
method (or its deprecated equivalents,show
andhide
). For example, a window might be miniaturized into an icon (iconified), without a component hidden event being generated.Component Event Methods
TheComponentListener
interface and its corresponding adapter class,ComponentAdapter
, contain four methods:
void componentHidden(ComponentEvent)
- Called by the AWT after the listened-to component is hidden as the result of the
setVisible
method being called.
void componentMoved(ComponentEvent)
- Called by the AWT after the listened-to component moves, relative to its container. For example, if a window is moved, the window generates a component moved event, but the components it contains do not.
void componentResized(ComponentEvent)
- Called by the AWT after the listened-to component's size (rectangular bounds) changes.
void componentShown(ComponentEvent)
- Called by the AWT after the listened-to component becomes visible as the result of the
setVisible
method being called.Examples of Handling Component Events
The following applet demonstrates component events. The applet contains a button that brings up a window (Frame
). The window contains a panel that has a label and a checkbox. The checkbox controls whether the label is visible. When you leave the applet's page, the window disappears; it reappears when you return to the applet's page. A text area displays a message every time the window, panel, label, or checkbox generates a component event.
Note: The above applet requires JDK 1.1. If you are using an older browser that does not support 1.1, you won't be able to run the applet. Instead, you need to view this page in a 1.1 browser, such as HotJava, the JDK Applet Viewer (appletviewer
), or certain versions of Netscape Navigator and Internet Explorer. For more information about running applets, refer to About Our Examples.
Try this:
- Click the button labeled "Start playing...".
The window comes up, generating one or more component-shown and component-moved events.- Click the checkbox to hide the label.
The label generates a component hidden event.- Click the checkbox again to show the label.
The label generates a component shown event.- Iconify and then deiconify the window labeled "SomeFrame".
You do not get component hidden or shown events.- Resize the window labeled "SomeFrame".
You'll see component resized (and possibly component moved) events from all three components -- label, checkbox, and window. If the window's layout manager didn't make every component as wide as possible, the label and checkbox wouldn't have been resized.
You can find the applet's code in
ComponentEventDemo.java
. Here is the applet's component event handling code:public class ComponentEventDemo ... implements ComponentListener { ... //where initialization occurs: aFrame = new Frame("A Frame"); ComponentPanel p = new ComponentPanel(this); aFrame.addComponentListener(this); p.addComponentListener(this); ... public void componentHidden(ComponentEvent e) { displayMessage("componentHidden event from " + e.getComponent().getClass().getName()); } public void componentMoved(ComponentEvent e) { displayMessage("componentMoved event from " + e.getComponent().getClass().getName()); } public void componentResized(ComponentEvent e) { displayMessage("componentResized event from " + e.getComponent().getClass().getName()); } public void componentShown(ComponentEvent e) { displayMessage("componentShown event from " + e.getComponent().getClass().getName()); } } class ComponentPanel extends Panel ... { ... ComponentPanel(ComponentEventDemo listener) { ...//after creating the label and checkbox: label.addComponentListener(listener); checkbox.addComponentListener(listener); } ... }The
ComponentEvent
ClassEach component event method has a single parameter: aComponentEvent
object. TheComponentEvent
class defines a useful method,getComponent
, which returns theComponent
that generated the event.
Using Components, the GUI Building Blocks |