src/share/classes/java/awt/Container.java

Print this page




1475      * @see #validate
1476      * @since JDK1.1
1477      */
1478     public void doLayout() {
1479         layout();
1480     }
1481 
1482     /**
1483      * @deprecated As of JDK version 1.1,
1484      * replaced by <code>doLayout()</code>.
1485      */
1486     @Deprecated
1487     public void layout() {
1488         LayoutManager layoutMgr = this.layoutMgr;
1489         if (layoutMgr != null) {
1490             layoutMgr.layoutContainer(this);
1491         }
1492     }
1493 
1494     /**
1495      * Invalidates the container.  The container and all parents
1496      * above it are marked as needing to be laid out.  This method can
1497      * be called often, so it needs to execute quickly.
1498      *
1499      * <p> If the {@code LayoutManager} installed on this container is
1500      * an instance of {@code LayoutManager2}, then
1501      * {@link LayoutManager2#invalidateLayout(Container)} is invoked on
1502      * it supplying this {@code Container} as the argument.







































1503      *
1504      * @see #validate
1505      * @see #layout
1506      * @see LayoutManager
1507      * @see LayoutManager2#invalidateLayout(Container)
1508      */

1509     public void invalidate() {
1510         LayoutManager layoutMgr = this.layoutMgr;
1511         if (layoutMgr instanceof LayoutManager2) {
1512             LayoutManager2 lm = (LayoutManager2) layoutMgr;
1513             lm.invalidateLayout(this);
1514         }
1515         super.invalidate();
1516     }
1517 
1518     /**
1519      * Validates this container and all of its subcomponents.
1520      * <p>
1521      * The <code>validate</code> method is used to cause a container
1522      * to lay out its subcomponents again. It should be invoked when
1523      * this container's subcomponents are modified (added to or
1524      * removed from the container, or layout-related information
1525      * changed) after the container has been displayed.
1526      *
1527      * <p>If this {@code Container} is not valid, this method invokes









1528      * the {@code validateTree} method and marks this {@code Container}
1529      * as valid. Otherwise, no action is performed.
1530      * <p>
1531      * Note that the {@code invalidate()} method may invalidate not only the
1532      * component it is called upon, but also the parents of the component.
1533      * Therefore, to restore the validity of the hierarchy, the {@code
1534      * validate()} method must be invoked on the top-most invalid container of
1535      * the hierarchy. For performance reasons a developer may postpone the
1536      * validation of the hierarchy till a bunch of layout-related operations
1537      * completes, e.g. after adding all the children to the container.
1538      *
1539      * @see #add(java.awt.Component)
1540      * @see #invalidate

1541      * @see javax.swing.JComponent#revalidate()
1542      * @see #validateTree
1543      */
1544     public void validate() {
1545         /* Avoid grabbing lock unless really necessary. */
1546         if (!isValid()) {
1547             boolean updateCur = false;
1548             synchronized (getTreeLock()) {
1549                 if (!isValid() && peer != null) {


1550                     ContainerPeer p = null;
1551                     if (peer instanceof ContainerPeer) {
1552                         p = (ContainerPeer) peer;
1553                     }
1554                     if (p != null) {
1555                         p.beginValidate();
1556                     }
1557                     validateTree();
1558                     if (p != null) {
1559                         p.endValidate();



1560                         updateCur = isVisible();
1561                     }
1562                 }
1563             }

1564             if (updateCur) {
1565                 updateCursorImmediately();
1566             }
1567         }
1568     }
1569 
1570     /**

































1571      * Recursively descends the container tree and recomputes the
1572      * layout for any subtrees marked as needing it (those marked as
1573      * invalid).  Synchronization should be provided by the method
1574      * that calls this one:  <code>validate</code>.
1575      *
1576      * @see #doLayout
1577      * @see #validate
1578      */
1579     protected void validateTree() {
1580         checkTreeLock();
1581         if (!isValid()) {
1582             if (peer instanceof ContainerPeer) {
1583                 ((ContainerPeer)peer).beginLayout();
1584             }

1585             doLayout();

1586             for (int i = 0; i < component.size(); i++) {
1587                 Component comp = component.get(i);
1588                 if (   (comp instanceof Container)
1589                        && !(comp instanceof Window)
1590                        && !comp.isValid()) {


1591                     ((Container)comp).validateTree();
1592                 } else {
1593                     comp.validate();
1594                 }
1595             }
1596             if (peer instanceof ContainerPeer) {
1597                 ((ContainerPeer)peer).endLayout();
1598             }
1599         }
1600         super.validate();
1601     }
1602 
1603     /**
1604      * Recursively descends the container tree and invalidates all
1605      * contained components.
1606      */
1607     void invalidateTree() {
1608         synchronized (getTreeLock()) {
1609             for (int i = 0; i < component.size(); i++) {
1610                 Component comp = component.get(i);




1475      * @see #validate
1476      * @since JDK1.1
1477      */
1478     public void doLayout() {
1479         layout();
1480     }
1481 
1482     /**
1483      * @deprecated As of JDK version 1.1,
1484      * replaced by <code>doLayout()</code>.
1485      */
1486     @Deprecated
1487     public void layout() {
1488         LayoutManager layoutMgr = this.layoutMgr;
1489         if (layoutMgr != null) {
1490             layoutMgr.layoutContainer(this);
1491         }
1492     }
1493 
1494     /**
1495      * Indicates if this container is a <i>validate root</i>.
1496      * <p>
1497      * Changes of layout-related information, such as bounds, of descendants of
1498      * a validate root do not affect the layout of the parent of the validate
1499      * root. This enables the {@code invalidate()} method to stop invalidating
1500      * the component hierarchy when the method encounters a validate root.
1501      * <p>
1502      * If a component hierarchy contains validate roots, the {@code validate()}
1503      * method must be invoked on the validate root of a previously invalidated
1504      * component, rather than on the top-level container (such as a {@code
1505      * Frame} object) to restore the validity of the hierarchy later.
1506      * <p>
1507      * The {@code Window} class and the {@code Applet} class are the validate
1508      * roots in AWT.  Swing introduces more validate roots.
1509      *
1510      * @return whether this container is a validate root
1511      * @see #invalidate
1512      * @see java.awt.Component#invalidate
1513      * @see javax.swing.JComponent#isValidateRoot
1514      * @see javax.swing.JComponent#revalidate
1515      * @since 1.7
1516      */
1517     public boolean isValidateRoot() {
1518         return false;
1519     }
1520 
1521     /**
1522      * Invalidates the parent of the container unless the container
1523      * is a validate root.
1524      */
1525     @Override
1526     void invalidateParent() {
1527         if (!isValidateRoot()) {
1528             super.invalidateParent();
1529         }
1530     }
1531 
1532     /**
1533      * Invalidates the container.
1534      * <p>
1535      * If the {@code LayoutManager} installed on this container is an instance
1536      * of the {@code LayoutManager2} interface, then
1537      * the {@link LayoutManager2#invalidateLayout(Container)} method is invoked
1538      * on it supplying this {@code Container} as the argument.
1539      * <p>
1540      * Afterwards this method marks this container invalid, and invalidates its
1541      * ancestors. See the {@link Component#invalidate} method for more details.
1542      *
1543      * @see #validate
1544      * @see #layout
1545      * @see LayoutManager2

1546      */
1547     @Override
1548     public void invalidate() {
1549         LayoutManager layoutMgr = this.layoutMgr;
1550         if (layoutMgr instanceof LayoutManager2) {
1551             LayoutManager2 lm = (LayoutManager2) layoutMgr;
1552             lm.invalidateLayout(this);
1553         }
1554         super.invalidate();
1555     }
1556 
1557     /**
1558      * Validates this container and all of its subcomponents.
1559      * <p>
1560      * Validating a container means laying out its subcomponents. Changes in
1561      * layout-related information, such as setting the bounds of a component,
1562      * or adding a component to the container, invalidate the container
1563      * automatically. Note that the ancestors of the container may be
1564      * invalidated also (see {@link Component#invalidate} for details.)
1565      * Therefore, to restore the validity of the hierarchy, the {@code
1566      * validate()} method should be invoked on a validate root of an
1567      * invalidated component, or on the top-most container if the hierarchy
1568      * does not contain validate roots.
1569      * <p>
1570      * Validating the container may be a quite time-consuming operation. For
1571      * performance reasons a developer may postpone the validation of the
1572      * hierarchy till a bunch of layout-related operations completes, e.g.
1573      * after adding all the children to the container.
1574      * <p>
1575      * If this {@code Container} is not valid, this method invokes
1576      * the {@code validateTree} method and marks this {@code Container}
1577      * as valid. Otherwise, no action is performed.








1578      *
1579      * @see #add(java.awt.Component)
1580      * @see #invalidate
1581      * @see Container#isValidateRoot
1582      * @see javax.swing.JComponent#revalidate()
1583      * @see #validateTree
1584      */
1585     public void validate() {
1586         /* Avoid grabbing lock unless really necessary. */
1587         if (!isValid() || descendUnconditionallyWhenValidating) {
1588             boolean updateCur = false;
1589             synchronized (getTreeLock()) {
1590                 if ((!isValid() || descendUnconditionallyWhenValidating)
1591                         && peer != null)
1592                 {
1593                     ContainerPeer p = null;
1594                     if (peer instanceof ContainerPeer) {
1595                         p = (ContainerPeer) peer;
1596                     }
1597                     if (p != null) {
1598                         p.beginValidate();
1599                     }
1600                     validateTree();
1601                     if (p != null) {
1602                         p.endValidate();
1603                         // Avoid updating cursor if this is an internal call.
1604                         // See validateUnconditionally() for details.
1605                         if (!descendUnconditionallyWhenValidating) {
1606                             updateCur = isVisible();
1607                         }
1608                     }
1609                 }
1610             }
1611             if (updateCur) {
1612                 updateCursorImmediately();
1613             }
1614         }
1615     }
1616 
1617     /**
1618      * Indicates whether valid containers should also traverse their
1619      * children and call validateTree() method on them.
1620      *
1621      * Synchronization: TreeLock.
1622      *
1623      * The field is allowed to be static as long as the TreeLock itself is
1624      * static.
1625      *
1626      * @see #validateUnconditionally()
1627      */
1628     private static boolean descendUnconditionallyWhenValidating = false;
1629 
1630     /**
1631      * Unconditionally validate the component hierarchy.
1632      */
1633     final void validateUnconditionally() {
1634         boolean updateCur = false;
1635         synchronized (getTreeLock()) {
1636             descendUnconditionallyWhenValidating = true;
1637 
1638             validate();
1639             if (peer instanceof ContainerPeer) {
1640                 updateCur = isVisible();
1641             }
1642 
1643             descendUnconditionallyWhenValidating = false;
1644         }
1645         if (updateCur) {
1646             updateCursorImmediately();
1647         }
1648     }
1649 
1650     /**
1651      * Recursively descends the container tree and recomputes the
1652      * layout for any subtrees marked as needing it (those marked as
1653      * invalid).  Synchronization should be provided by the method
1654      * that calls this one:  <code>validate</code>.
1655      *
1656      * @see #doLayout
1657      * @see #validate
1658      */
1659     protected void validateTree() {
1660         checkTreeLock();
1661         if (!isValid() || descendUnconditionallyWhenValidating) {
1662             if (peer instanceof ContainerPeer) {
1663                 ((ContainerPeer)peer).beginLayout();
1664             }
1665             if (!isValid()) {
1666                 doLayout();
1667             }
1668             for (int i = 0; i < component.size(); i++) {
1669                 Component comp = component.get(i);
1670                 if (   (comp instanceof Container)
1671                        && !(comp instanceof Window)
1672                        && (!comp.isValid() ||
1673                            descendUnconditionallyWhenValidating))
1674                 {
1675                     ((Container)comp).validateTree();
1676                 } else {
1677                     comp.validate();
1678                 }
1679             }
1680             if (peer instanceof ContainerPeer) {
1681                 ((ContainerPeer)peer).endLayout();
1682             }
1683         }
1684         super.validate();
1685     }
1686 
1687     /**
1688      * Recursively descends the container tree and invalidates all
1689      * contained components.
1690      */
1691     void invalidateTree() {
1692         synchronized (getTreeLock()) {
1693             for (int i = 0; i < component.size(); i++) {
1694                 Component comp = component.get(i);