How to Write a Component Listener

 < Day Day Up > 

One or more component events are fired by a Component 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 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-shown events occur only as the result of calls to a Component 's setVisible method. For example, a window might be miniaturized into an icon ( iconified ) without a component-hidden event being fired.

Figure 2 demonstrates component events. The window contains a panel that has a label and a check box. The check box controls whether the label is visible. A text area displays a message every time the window, panel, label, or check box fires a component event.

Figure 2. The ComponentEventDemo application.

graphics/10fig02.gif

Try This:

  1. graphics/cd_icon.gif

    Run ComponentEventDemo using Java Web Start or compile and run the example. [5] When the window appears, one or more component-shown events have been fired.

    [5] To run ComponentEventDemo using Java Web Start, click the ComponentEventDemo link on the RunExamples/events.html page on the CD. You can find the source files here: JavaTutorial/uiswing/events/example-1dot4/index.html#ComponentEventDemo .

  2. Click the check box to hide the label. The label fires a component-hidden event. The panel fires component-moved and component-resized events. The check box fires a component-moved event.

  3. Click the check box again to show the label. The label fires a component-shown event. The panel fires component-moved and component-resized events. The check box fires a component-moved event.

  4. Iconify and then deiconify the window. You do not get component-hidden or -shown events. If you want to be notified of iconification events, you should use a window listener or a window state listener.

  5. Resize the window. You'll see component-resized (and possibly component-moved) events from all four components label, check box, panel, and frame. If the responsible layout managers didn't make every component as wide as possible, the panel, label, and check box wouldn't have been resized.

You can find the demo's code in ComponentEventDemo.java . Here's just the code related to handling component events:

 public class ComponentEventDemo ... implements ComponentListener {     static JFrame frame;     JLabel label;     ...     public ComponentEventDemo() {         ...         JPanel panel = new JPanel(new BorderLayout());         label = new JLabel("This is a label", JLabel.CENTER);         label.addComponentListener(this);         panel.add(label, BorderLayout.CENTER);         JCheckBox checkbox = new JCheckBox("Label visible", true);         checkbox.addComponentListener(this);         panel.add(checkbox, BorderLayout.PAGE_END);         panel.addComponentListener(this);         ...         frame.addComponentListener(this);     }     ...     public void componentHidden(ComponentEvent e) {         displayMessage("componentHidden event from "                        + e.getComponent().getClass().getName());     }     public void componentMoved(ComponentEvent e) {         Component c = e.getComponent();         displayMessage("componentMoved event from "                        + c.getClass().getName()                        + "; new location: "                        + c.getLocation().x                        + ", "                        + c.getLocation().y);     }     public void componentResized(ComponentEvent e) {         Component c = e.getComponent();         displayMessage("componentResized event from "                        + c.getClass().getName()                        + "; new size: "                        + c.getSize().width                        + ", "                        + c.getSize().height);     }     public void componentShown(ComponentEvent e) {         displayMessage("componentShown event from "                        + e.getComponent().getClass().getName());     }  //Where the GUI is initialized:  JComponent newContentPane = new ComponentEventDemo();     newContentPane.setOpaque(true); //content panes must be opaque     frame.setContentPane(newContentPane);     ... } 

The Component Listener API

Table 7 lists the methods in the ComponentListener interface and Table 8 describes the method in the ComponentEvent class. Also refer to the ComponentListener API documentation at: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/Component-Event.html. The ComponentEvent API documentation is online at: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/ComponentEvent.html.

Table 7. The ComponentListener Interface (All these methods are also in the adapter class, ComponentAdapter . [a] )

Method

Purpose

componentHidden(ComponentEvent)

Called after the listened-to component is hidden as the result of the setVisible method being called.

componentMoved(ComponentEvent)

Called after the listened-to component moves, relative to its container. For example, if a window is moved, the window fires a component-moved event, but the components it contains do not.

componentResized(ComponentEvent)

Called after the listened-to component's size (rectangular bounds) changes.

componentShown(ComponentEvent)

Called after the listened-to component becomes visible as the result of the setVisible method being called.

[a] ComponentAdapter API documentation: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/ComponentAdapter.html.

Table 8. The ComponentEvent Class

Method

Purpose

Component getComponent()

Return the component that fired the event. You can use this instead of the getSource method.

Examples That Use Component Listeners

The following examples use component listeners.

Example

Where Described

Notes

ComponentEventDemo

This section

Reports all component events that occur on several components to demonstrate the circumstances under which component events are fired.

 < Day Day Up > 


JFC Swing Tutorial, The. A Guide to Constructing GUIs
The JFC Swing Tutorial: A Guide to Constructing GUIs (2nd Edition)
ISBN: 0201914670
EAN: 2147483647
Year: 2004
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net