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      *


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


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


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


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


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