javacodex.com
Java Examples
Java Examples
memu home questions

ComponentListener

The ComponentEvent occurs when a component’s visibility, size, or location is changed.

Source: (ComponentListenerExample.java)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
public class ComponentListenerExample {
  public static void main(String [] args) {
 
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("ComponentListener Example");
    frame.setSize(300,150);
 
    final JLabel label = new JLabel();
    frame.add(label);
 
    frame.addComponentListener(new ComponentListener() {
        public void componentHidden(ComponentEvent ce) {
          label.setText("Component Hidden");
        }
        public void componentShown(ComponentEvent ce) {
          label.setText("Component Shown");
        }
        public void componentMoved(ComponentEvent ce) {
          label.setText("Component Moved");
        }
        public void componentResized(ComponentEvent ce) {
          label.setText("Component Resized");
        }
    });
 
    frame.setVisible(true);			
  }
}
 

Output:

$ java ComponentListenerExample
Output



Contact: javacodex@yahoo.com