ComponentListener Applet
ComponentListener is used to detect and handle changes/updates done on any components like Button, MenuItem, JToggleButton, JTextField, any…
ComponentListener Interface
As per the event delegation model we are going to implement this interface in our class. ComponentListener consists of 4 methods which are as below.
- public void componentHidden(ComponentEvent e)
- public void componentMoved(ComponentEvent e)
- public void componentResized(ComponentEvent e)
- public void componentShown(ComponentEvent e)
To add ComponentListener on any component the syntax is: btn.addComponentListener(this);
For further reference you can refer my article on Event Handling Methodologies.
Program:
import java.awt.*; import java.awt.event.*; import java.applet.*; public class AppletComponentListener extends Applet implements ComponentListener { TextArea ta = new TextArea(7,50); public void init() { Button left = new Button("Left"); left.setName("Left"); left.addComponentListener(this); final Button right = new Button("Right"); right.setName("Right"); right.addComponentListener(this); ActionListener action = new ActionListener() { public void actionPerformed(ActionEvent e) { right.setVisible(!right.isVisible()); } }; left.addActionListener(action); add(left); add(right); add(ta); } public void componentHidden(ComponentEvent e) { dump("Hidden", e); } public void componentMoved(ComponentEvent e) { dump("Moved", e); } public void componentResized(ComponentEvent e) { dump("Resized", e); } public void componentShown(ComponentEvent e) { dump("Shown", e); } private void dump(String type, ComponentEvent e) { ta.append(e.getComponent().getName() + " : " + type+"\n"); } } /* <applet code="AppletComponentListener" width="400" height="250"></applet> */
Recent Comments