When you add/remove a component to/from a container, the container is marked invalid. Before the container is made visible for the first time, the container is validated. The validation process of a container computes the size and location of all children in its containment hierarchy. Consider the following the snippet of code to show a frame:

MyFrame frame = new MyFrame("Test Frame");frame.pack();frame.setVisible(true);

The pack() method does two things:

  • First, it computes the size and position of all children of the frame (that is, validates the frame).
  • Second, it resizes the frame, so its children just fit into it.

The setVisible() method in the code is smart enough not to validate the frame again because the pack() method has already validated ...

Get Beginning Java 8 APIs, Extensions and Libraries Swing, JavaFX, JavaScript, JDBC and Network Programming APIs now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.