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

Print this page


   1 /*
   2  * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 293     @SuppressWarnings({"unchecked","rawtypes"})
 294     void initializeFocusTraversalKeys() {
 295         focusTraversalKeys = new Set[4];
 296     }
 297 
 298     /**
 299      * Gets the number of components in this panel.
 300      * <p>
 301      * Note: This method should be called under AWT tree lock.
 302      *
 303      * @return    the number of components in this panel.
 304      * @see       #getComponent
 305      * @since     JDK1.1
 306      * @see Component#getTreeLock()
 307      */
 308     public int getComponentCount() {
 309         return countComponents();
 310     }
 311 
 312     /**


 313      * @deprecated As of JDK version 1.1,
 314      * replaced by getComponentCount().
 315      */
 316     @Deprecated
 317     public int countComponents() {
 318         // This method is not synchronized under AWT tree lock.
 319         // Instead, the calling code is responsible for the
 320         // synchronization. See 6784816 for details.
 321         return component.size();
 322     }
 323 
 324     /**
 325      * Gets the nth component in this container.
 326      * <p>
 327      * Note: This method should be called under AWT tree lock.
 328      *
 329      * @param      n   the index of the component to get.
 330      * @return     the n<sup>th</sup> component in this container.
 331      * @exception  ArrayIndexOutOfBoundsException
 332      *                 if the n<sup>th</sup> value does not exist.


 374             return getComponents();
 375         }
 376     }
 377 
 378     /**
 379      * Determines the insets of this container, which indicate the size
 380      * of the container's border.
 381      * <p>
 382      * A <code>Frame</code> object, for example, has a top inset that
 383      * corresponds to the height of the frame's title bar.
 384      * @return    the insets of this container.
 385      * @see       Insets
 386      * @see       LayoutManager
 387      * @since     JDK1.1
 388      */
 389     public Insets getInsets() {
 390         return insets();
 391     }
 392 
 393     /**

 394      * @deprecated As of JDK version 1.1,
 395      * replaced by <code>getInsets()</code>.

 396      */
 397     @Deprecated
 398     public Insets insets() {
 399         ComponentPeer peer = this.peer;
 400         if (peer instanceof ContainerPeer) {
 401             ContainerPeer cpeer = (ContainerPeer)peer;
 402             return (Insets)cpeer.getInsets().clone();
 403         }
 404         return new Insets(0, 0, 0, 0);
 405     }
 406 
 407     /**
 408      * Appends the specified component to the end of this container.
 409      * This is a convenience method for {@link #addImpl}.
 410      * <p>
 411      * This method changes layout-related information, and therefore,
 412      * invalidates the component hierarchy. If the container has already been
 413      * displayed, the hierarchy must be validated thereafter in order to
 414      * display the added component.
 415      *


 421      * @see javax.swing.JComponent#revalidate()
 422      * @return    the component argument
 423      */
 424     public Component add(Component comp) {
 425         addImpl(comp, null, -1);
 426         return comp;
 427     }
 428 
 429     /**
 430      * Adds the specified component to this container.
 431      * This is a convenience method for {@link #addImpl}.
 432      * <p>
 433      * This method is obsolete as of 1.1.  Please use the
 434      * method <code>add(Component, Object)</code> instead.
 435      * <p>
 436      * This method changes layout-related information, and therefore,
 437      * invalidates the component hierarchy. If the container has already been
 438      * displayed, the hierarchy must be validated thereafter in order to
 439      * display the added component.
 440      *



 441      * @exception NullPointerException if {@code comp} is {@code null}
 442      * @see #add(Component, Object)
 443      * @see #invalidate
 444      */
 445     public Component add(String name, Component comp) {
 446         addImpl(comp, name, -1);
 447         return comp;
 448     }
 449 
 450     /**
 451      * Adds the specified component to this container at the given
 452      * position.
 453      * This is a convenience method for {@link #addImpl}.
 454      * <p>
 455      * This method changes layout-related information, and therefore,
 456      * invalidates the component hierarchy. If the container has already been
 457      * displayed, the hierarchy must be validated thereafter in order to
 458      * display the added component.
 459      *
 460      *


1456 
1457     final void createChildHierarchyEvents(int id, long changeFlags,
1458         boolean enabledOnToolkit)
1459     {
1460         checkTreeLock();
1461         if (component.isEmpty()) {
1462             return;
1463         }
1464         int listeners = getListenersCount(id, enabledOnToolkit);
1465 
1466         for (int count = listeners, i = 0; count > 0; i++) {
1467             count -= component.get(i).createHierarchyEvents(id, this, parent,
1468                 changeFlags, enabledOnToolkit);
1469         }
1470     }
1471 
1472     /**
1473      * Gets the layout manager for this container.
1474      * @see #doLayout
1475      * @see #setLayout

1476      */
1477     public LayoutManager getLayout() {
1478         return layoutMgr;
1479     }
1480 
1481     /**
1482      * Sets the layout manager for this container.
1483      * <p>
1484      * This method changes layout-related information, and therefore,
1485      * invalidates the component hierarchy.
1486      *
1487      * @param mgr the specified layout manager
1488      * @see #doLayout
1489      * @see #getLayout
1490      * @see #invalidate
1491      */
1492     public void setLayout(LayoutManager mgr) {
1493         layoutMgr = mgr;
1494         invalidateIfValid();
1495     }


3837         /**
3838          * Returns the <code>Accessible</code> child, if one exists,
3839          * contained at the local coordinate <code>Point</code>.
3840          *
3841          * @param p the point defining the top-left corner of the
3842          *    <code>Accessible</code>, given in the coordinate space
3843          *    of the object's parent
3844          * @return the <code>Accessible</code>, if it exists,
3845          *    at the specified location; else <code>null</code>
3846          */
3847         public Accessible getAccessibleAt(Point p) {
3848             return Container.this.getAccessibleAt(p);
3849         }
3850 
3851         /**
3852          * Number of PropertyChangeListener objects registered. It's used
3853          * to add/remove ContainerListener to track target Container's state.
3854          */
3855         private volatile transient int propertyListenersCount = 0;
3856 



3857         protected ContainerListener accessibleContainerHandler = null;
3858 
3859         /**
3860          * Fire <code>PropertyChange</code> listener, if one is registered,
3861          * when children are added or removed.
3862          * @since 1.3
3863          */
3864         protected class AccessibleContainerHandler
3865             implements ContainerListener {
3866             public void componentAdded(ContainerEvent e) {
3867                 Component c = e.getChild();
3868                 if (c != null && c instanceof Accessible) {
3869                     AccessibleAWTContainer.this.firePropertyChange(
3870                         AccessibleContext.ACCESSIBLE_CHILD_PROPERTY,
3871                         null, ((Accessible) c).getAccessibleContext());
3872                 }
3873             }
3874             public void componentRemoved(ContainerEvent e) {
3875                 Component c = e.getChild();
3876                 if (c != null && c instanceof Accessible) {


   1 /*
   2  * Copyright (c) 1995, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 293     @SuppressWarnings({"unchecked","rawtypes"})
 294     void initializeFocusTraversalKeys() {
 295         focusTraversalKeys = new Set[4];
 296     }
 297 
 298     /**
 299      * Gets the number of components in this panel.
 300      * <p>
 301      * Note: This method should be called under AWT tree lock.
 302      *
 303      * @return    the number of components in this panel.
 304      * @see       #getComponent
 305      * @since     JDK1.1
 306      * @see Component#getTreeLock()
 307      */
 308     public int getComponentCount() {
 309         return countComponents();
 310     }
 311 
 312     /**
 313      * Returns the number of components in this container.
 314      * @return  the number of components in this container
 315      * @deprecated As of JDK version 1.1,
 316      * replaced by getComponentCount().
 317      */
 318     @Deprecated
 319     public int countComponents() {
 320         // This method is not synchronized under AWT tree lock.
 321         // Instead, the calling code is responsible for the
 322         // synchronization. See 6784816 for details.
 323         return component.size();
 324     }
 325 
 326     /**
 327      * Gets the nth component in this container.
 328      * <p>
 329      * Note: This method should be called under AWT tree lock.
 330      *
 331      * @param      n   the index of the component to get.
 332      * @return     the n<sup>th</sup> component in this container.
 333      * @exception  ArrayIndexOutOfBoundsException
 334      *                 if the n<sup>th</sup> value does not exist.


 376             return getComponents();
 377         }
 378     }
 379 
 380     /**
 381      * Determines the insets of this container, which indicate the size
 382      * of the container's border.
 383      * <p>
 384      * A <code>Frame</code> object, for example, has a top inset that
 385      * corresponds to the height of the frame's title bar.
 386      * @return    the insets of this container.
 387      * @see       Insets
 388      * @see       LayoutManager
 389      * @since     JDK1.1
 390      */
 391     public Insets getInsets() {
 392         return insets();
 393     }
 394 
 395     /**
 396      * Returns the insets for this container.
 397      * @deprecated As of JDK version 1.1,
 398      * replaced by <code>getInsets()</code>.
 399      * @return  the insets for this container
 400      */
 401     @Deprecated
 402     public Insets insets() {
 403         ComponentPeer peer = this.peer;
 404         if (peer instanceof ContainerPeer) {
 405             ContainerPeer cpeer = (ContainerPeer)peer;
 406             return (Insets)cpeer.getInsets().clone();
 407         }
 408         return new Insets(0, 0, 0, 0);
 409     }
 410 
 411     /**
 412      * Appends the specified component to the end of this container.
 413      * This is a convenience method for {@link #addImpl}.
 414      * <p>
 415      * This method changes layout-related information, and therefore,
 416      * invalidates the component hierarchy. If the container has already been
 417      * displayed, the hierarchy must be validated thereafter in order to
 418      * display the added component.
 419      *


 425      * @see javax.swing.JComponent#revalidate()
 426      * @return    the component argument
 427      */
 428     public Component add(Component comp) {
 429         addImpl(comp, null, -1);
 430         return comp;
 431     }
 432 
 433     /**
 434      * Adds the specified component to this container.
 435      * This is a convenience method for {@link #addImpl}.
 436      * <p>
 437      * This method is obsolete as of 1.1.  Please use the
 438      * method <code>add(Component, Object)</code> instead.
 439      * <p>
 440      * This method changes layout-related information, and therefore,
 441      * invalidates the component hierarchy. If the container has already been
 442      * displayed, the hierarchy must be validated thereafter in order to
 443      * display the added component.
 444      *
 445      * @param name  the name of the component to be added
 446      * @param comp  the component to be added
 447      * @return  the component that was added
 448      * @exception NullPointerException if {@code comp} is {@code null}
 449      * @see #add(Component, Object)
 450      * @see #invalidate
 451      */
 452     public Component add(String name, Component comp) {
 453         addImpl(comp, name, -1);
 454         return comp;
 455     }
 456 
 457     /**
 458      * Adds the specified component to this container at the given
 459      * position.
 460      * This is a convenience method for {@link #addImpl}.
 461      * <p>
 462      * This method changes layout-related information, and therefore,
 463      * invalidates the component hierarchy. If the container has already been
 464      * displayed, the hierarchy must be validated thereafter in order to
 465      * display the added component.
 466      *
 467      *


1463 
1464     final void createChildHierarchyEvents(int id, long changeFlags,
1465         boolean enabledOnToolkit)
1466     {
1467         checkTreeLock();
1468         if (component.isEmpty()) {
1469             return;
1470         }
1471         int listeners = getListenersCount(id, enabledOnToolkit);
1472 
1473         for (int count = listeners, i = 0; count > 0; i++) {
1474             count -= component.get(i).createHierarchyEvents(id, this, parent,
1475                 changeFlags, enabledOnToolkit);
1476         }
1477     }
1478 
1479     /**
1480      * Gets the layout manager for this container.
1481      * @see #doLayout
1482      * @see #setLayout
1483      * @return  the current layout manager for this container
1484      */
1485     public LayoutManager getLayout() {
1486         return layoutMgr;
1487     }
1488 
1489     /**
1490      * Sets the layout manager for this container.
1491      * <p>
1492      * This method changes layout-related information, and therefore,
1493      * invalidates the component hierarchy.
1494      *
1495      * @param mgr the specified layout manager
1496      * @see #doLayout
1497      * @see #getLayout
1498      * @see #invalidate
1499      */
1500     public void setLayout(LayoutManager mgr) {
1501         layoutMgr = mgr;
1502         invalidateIfValid();
1503     }


3845         /**
3846          * Returns the <code>Accessible</code> child, if one exists,
3847          * contained at the local coordinate <code>Point</code>.
3848          *
3849          * @param p the point defining the top-left corner of the
3850          *    <code>Accessible</code>, given in the coordinate space
3851          *    of the object's parent
3852          * @return the <code>Accessible</code>, if it exists,
3853          *    at the specified location; else <code>null</code>
3854          */
3855         public Accessible getAccessibleAt(Point p) {
3856             return Container.this.getAccessibleAt(p);
3857         }
3858 
3859         /**
3860          * Number of PropertyChangeListener objects registered. It's used
3861          * to add/remove ContainerListener to track target Container's state.
3862          */
3863         private volatile transient int propertyListenersCount = 0;
3864 
3865         /**
3866          * The handler to fire {@code PropertyChange} when children are added or removed
3867          */
3868         protected ContainerListener accessibleContainerHandler = null;
3869 
3870         /**
3871          * Fire <code>PropertyChange</code> listener, if one is registered,
3872          * when children are added or removed.
3873          * @since 1.3
3874          */
3875         protected class AccessibleContainerHandler
3876             implements ContainerListener {
3877             public void componentAdded(ContainerEvent e) {
3878                 Component c = e.getChild();
3879                 if (c != null && c instanceof Accessible) {
3880                     AccessibleAWTContainer.this.firePropertyChange(
3881                         AccessibleContext.ACCESSIBLE_CHILD_PROPERTY,
3882                         null, ((Accessible) c).getAccessibleContext());
3883                 }
3884             }
3885             public void componentRemoved(ContainerEvent e) {
3886                 Component c = e.getChild();
3887                 if (c != null && c instanceof Accessible) {