![]() ![]() ![]() ![]() |
GUI Changes: The AWT Grows Up |
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
ComponentListener
interface and its corresponding adapter class,
ComponentAdapter
, contain four methods:
void componentHidden(ComponentEvent)
- Notifies the listener that the component is no longer visible. The component hidden and visible events correspond exactly to a
Component
'ssetVisible
method being called. Just because you can't see a component onscreen doesn't mean it's hidden. For example, a window might be miniaturized into an icon (iconified), without a component hidden event being generated. [CHECK]
void componentMoved(ComponentEvent)
- Notifies the listener that the component has moved, 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)
- Notifies the listener that the component's size [rectangular bounds?] has changed.
void componentShown(ComponentEvent)
- Notifies the listener that the component has become visible as the result of the
setVisible
method being called. [CHECK]The following applet demonstrates component events. The applet brings up a window (
Frame
) that contains a label and a checkbox. The checkbox controls whether the label is visible. Whenever the applet starts up (such as when you visit or revisit the page containing the applet), the window is made visible. Whenever the applet stops (such as when you leave the page containing the applet), the window is hidden. A text area displays a message every time the window, label, or checkbox generates a component event.
Try this:
- 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 ComponentDemo.java. Here is the applet's component event handling code:
public class ComponentDemo ... implements ComponentListener { ... //where initialization occurs: someFrame = new SomeFrame(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 SomeFrame extends Frame ... { ... SomeFrame(ComponentListener listener) { ... label.addComponentListener(listener); checkbox.addComponentListener(listener); this.addComponentListener(listener); ... } ... }You can find more examples of component listeners in the following sections: [LIST GOES HERE]
[Somewhere on this page should talk about ComponentEvent, link to its API doc.]
![]() ![]() ![]() ![]() |
GUI Changes: The AWT Grows Up |