Determining When a Component Has Been Made Visible, Moved, or Resized : JFrame States « Swing « Java Tutorial






import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JFrame;

public class Main {
  public static void main() {

    ComponentListener listener = new ComponentAdapter() {
      public void componentShown(ComponentEvent evt) {
        Component c = (Component) evt.getSource();
        System.out.println("Component is now visible");
      }

      public void componentHidden(ComponentEvent evt) {
        Component c = (Component) evt.getSource();
        System.out.println("Component is now hidden");
      }

      public void componentMoved(ComponentEvent evt) {
        Component c = (Component) evt.getSource();

        Point newLoc = c.getLocation();
        System.out.println("Get new location");
      }

      public void componentResized(ComponentEvent evt) {
        Component c = (Component) evt.getSource();

        Dimension newSize = c.getSize();
        System.out.println("Get new size");
      }
    };
    JFrame frame = new JFrame();
    frame.setSize(300, 300);
    frame.addComponentListener(listener);
    frame.setVisible(true);
  }
}








14.81.JFrame States
14.81.1.Setting the Bounds for a Maximized Frame
14.81.2.Iconifies a frame; the maximized bits are not affected.
14.81.3.Deiconifies a frame; the maximized bits are not affected.
14.81.4.Maximizes a frame; the iconified bit is not affected
14.81.5.Determining When a Frame or Window Is Opened or Closed
14.81.6.Determining When a Frame or Window Is Iconized or Maximized
14.81.7.Determining When a Component Has Been Made Visible, Moved, or Resized