Using ComponentListener to catch the JFrame Maximization event : ContainerListener « Swing Event « Java Tutorial






import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JFrame;

public class Main extends JFrame implements ComponentListener {
  public Main() {
    addComponentListener(this);
  }

  public void componentHidden(ComponentEvent e) {
    System.out.println("componentHidden");
  }

  public void componentMoved(ComponentEvent e) {
    System.out.println("componentMoved");
  }

  public void componentResized(ComponentEvent e) {
    System.out.println("componentResized");

    if (getState() == Frame.ICONIFIED) {
      System.out.println("RESIZED TO ICONIFIED");
    } else if (getState() == Frame.NORMAL) {
      System.out.println("RESIZED TO NORMAL");
    } else {
      System.out.println("RESIZED TO MAXIMIZED");
    }
  }

  public void componentShown(ComponentEvent e) {
  }

  public static void main(String[] arg) {
    Main m = new Main();

    m.setVisible(true);
    m.setSize(new Dimension(300, 100));
    m.setLocation(50, 50);
  }
}








15.11.ContainerListener
15.11.1.How to Write a Container ListenerHow to Write a Container Listener
15.11.2.ContainerListener and ContainerEventContainerListener and ContainerEvent
15.11.3.Using ComponentListener to catch the JFrame Maximization event
15.11.4.Determining When a Component Is Added or Removed from a Container