Component[] components = p.getComponents(); for (int index = 0; index < components.length; index++) { Component c = components[index]; Constraints constraints = getConstraints(c); int x = constraints.getX().getPreferredValue(); int y = constraints.getY().getPreferredValue(); int width = constraints.getWidth().getPreferredValue(); int height = constraints.getHeight().getPreferredValue();
int maxX = cs.getWidth().getPreferredValue() + insets.left + insets.right; int maxY = cs.getHeight().getPreferredValue() + insets.top + insets.bottom;
  public Dimension preferredLayoutSize(Container p)  {    int maxX = 0;    int maxY = 0;    int offsetX = p.getInsets().left;    int offsetY = p.getInsets().right;    Component[] components = p.getComponents();    for (int index = 0; index < components.length; index++)      {        Component c = components[index];        Constraints constraints = getConstraints(c);        int x = constraints.getX().getPreferredValue();        int y = constraints.getY().getPreferredValue();        int width = constraints.getWidth().getPreferredValue();        int height = constraints.getHeight().getPreferredValue();        int rightEdge = offsetX + x + width;        if (rightEdge > maxX)          maxX = rightEdge;        int bottomEdge = offsetY + y + height;        if (bottomEdge > maxY)          maxY = bottomEdge;      }    return new Dimension(maxX, maxY);  }