The class which processes the ComponentEvent should implement this interface.The object of that class must be registered with a component. The object can be registered using the addComponentListener() method. Component event are raised for information only.

Interface declaration

Following is the declaration for java.awt.event.ComponentListener interface:

  1. public interface ComponentListener
  2. extends EventListener

Interface methods

S.N.Method & Description
1 void componentHidden(ComponentEvent e) Invoked when the component has been made invisible.
2 void componentMoved(ComponentEvent e) Invoked when the component's position changes.
3 void componentResized(ComponentEvent e) Invoked when the component's size changes.
4 void componentShown(ComponentEvent e) Invoked when the component has been made visible.

Methods inherited

This interface inherits methods from the following interfaces:

  • java.awt.EventListener

ComponentListener Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui > AwtListenerDemo.java

  1. package com.tutorialspoint.gui;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class AwtListenerDemo {
  7. private Frame mainFrame;
  8. private Label headerLabel;
  9. private Label statusLabel;
  10. private Panel controlPanel;
  11.  
  12. public AwtListenerDemo(){
  13. prepareGUI();
  14. }
  15.  
  16. public static void main(String[] args){
  17. AwtListenerDemo awtListenerDemo = new AwtListenerDemo();
  18. awtListenerDemo.showComponentListenerDemo();
  19. }
  20.  
  21. private void prepareGUI(){
  22. mainFrame = new Frame("Java AWT Examples");
  23. mainFrame.setSize(400,400);
  24. mainFrame.setLayout(new GridLayout(3, 1));
  25. mainFrame.addWindowListener(new WindowAdapter() {
  26. public void windowClosing(WindowEvent windowEvent){
  27. System.exit(0);
  28. }
  29. });
  30.  
  31. headerLabel = new Label();
  32. headerLabel.setAlignment(Label.CENTER);
  33. statusLabel = new Label();
  34. statusLabel.setAlignment(Label.CENTER);
  35. statusLabel.setSize(350,100);
  36.  
  37. controlPanel = new Panel();
  38. controlPanel.setLayout(new FlowLayout());
  39.  
  40. mainFrame.add(headerLabel);
  41. mainFrame.add(controlPanel);
  42. mainFrame.add(statusLabel);
  43. mainFrame.setVisible(true);
  44. }
  45.  
  46. private void showComponentListenerDemo(){
  47. headerLabel.setText("Listener in action: ComponentListener");
  48.  
  49. ScrollPane panel = new ScrollPane();
  50. panel.setBackground(Color.magenta);
  51.  
  52. Label msglabel = new Label();
  53. msglabel.setAlignment(Label.CENTER);
  54. msglabel.setText("Welcome to TutorialsPoint AWT Tutorial.");
  55. panel.add(msglabel);
  56.  
  57. msglabel.addComponentListener(new CustomComponentListener());
  58. controlPanel.add(panel);
  59. mainFrame.setVisible(true);
  60. }
  61.  
  62. class CustomComponentListener implements ComponentListener {
  63.  
  64. public void componentResized(ComponentEvent e) {
  65. statusLabel.setText(statusLabel.getText()
  66. + e.getComponent().getClass().getSimpleName() + " resized. ");
  67. }
  68.  
  69. public void componentMoved(ComponentEvent e) {
  70. statusLabel.setText(statusLabel.getText()
  71. + e.getComponent().getClass().getSimpleName() + " moved. ");
  72. }
  73.  
  74. public void componentShown(ComponentEvent e) {
  75. statusLabel.setText(statusLabel.getText()
  76. + e.getComponent().getClass().getSimpleName() + " shown. ");
  77. }
  78.  
  79. public void componentHidden(ComponentEvent e) {
  80. statusLabel.setText(statusLabel.getText()
  81. + e.getComponent().getClass().getSimpleName() + " hidden. ");
  82. }
  83. }
  84. }

Compile the program using command prompt. Go to D:/ > AWT and type the following command.

  1. D:\AWT>javac com\tutorialspoint\gui\AwtListenerDemo.java

If no error comes that means compilation is successful. Run the program using following command.

  1. D:\AWT>java com.tutorialspoint.gui.AwtListenerDemo

Verify the following output

awt_componentlistener.jpg