< 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.
Try This:
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 APITable 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] )
Table 8. The ComponentEvent Class
Examples That Use Component ListenersThe following examples use component listeners.
|
< Day Day Up > |