src/java.desktop/share/classes/javax/swing/JLayeredPane.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 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
  23  * questions.
  24  */
  25 package javax.swing;
  26 
  27 import java.awt.Component;
  28 import java.util.ArrayList;
  29 import java.util.Hashtable;
  30 import java.awt.Color;
  31 import java.awt.Graphics;
  32 import java.awt.Rectangle;



  33 import sun.awt.SunToolkit;
  34 
  35 import javax.accessibility.*;
  36 
  37 /**
  38  * <code>JLayeredPane</code> adds depth to a JFC/Swing container,
  39  * allowing components to overlap each other when needed.
  40  * An <code>Integer</code> object specifies each component's depth in the
  41  * container, where higher-numbered components sit &quot;on top&quot; of other
  42  * components.
  43  * For task-oriented documentation and examples of using layered panes see
  44  * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html">How to Use a Layered Pane</a>,
  45  * a section in <em>The Java Tutorial</em>.
  46  *
  47  * <TABLE STYLE="FLOAT:RIGHT" BORDER="0" SUMMARY="layout">
  48  * <TR>
  49  *   <TD ALIGN="CENTER">
  50  *     <P STYLE="TEXT-ALIGN:CENTER"><IMG SRC="doc-files/JLayeredPane-1.gif"
  51  *     alt="The following text describes this image."
  52  *     WIDTH="269" HEIGHT="264" STYLE="FLOAT:BOTTOM; BORDER=0">


 138  * will affect all child components of this container without regard for
 139  * layer settings.
 140  * <p>
 141  * <strong>Warning:</strong> Swing is not thread safe. For more
 142  * information see <a
 143  * href="package-summary.html#threading">Swing's Threading
 144  * Policy</a>.
 145  * <p>
 146  * <strong>Warning:</strong>
 147  * Serialized objects of this class will not be compatible with
 148  * future Swing releases. The current serialization support is
 149  * appropriate for short term storage or RMI between applications running
 150  * the same version of Swing.  As of 1.4, support for long term storage
 151  * of all JavaBeans&trade;
 152  * has been added to the <code>java.beans</code> package.
 153  * Please see {@link java.beans.XMLEncoder}.
 154  *
 155  * @author David Kloba
 156  * @since 1.2
 157  */

 158 @SuppressWarnings("serial")
 159 public class JLayeredPane extends JComponent implements Accessible {
 160     /// Watch the values in getObjectForLayer()
 161     /** Convenience object defining the Default layer. Equivalent to new Integer(0).*/
 162     public final static Integer DEFAULT_LAYER = 0;
 163     /** Convenience object defining the Palette layer. Equivalent to new Integer(100).*/
 164     public final static Integer PALETTE_LAYER = 100;
 165     /** Convenience object defining the Modal layer. Equivalent to new Integer(200).*/
 166     public final static Integer MODAL_LAYER = 200;
 167     /** Convenience object defining the Popup layer. Equivalent to new Integer(300).*/
 168     public final static Integer POPUP_LAYER = 300;
 169     /** Convenience object defining the Drag layer. Equivalent to new Integer(400).*/
 170     public final static Integer DRAG_LAYER = 400;
 171     /** Convenience object defining the Frame Content layer.
 172       * This layer is normally only use to position the contentPane and menuBar
 173       * components of JFrame.
 174       * Equivalent to new Integer(-30000).
 175       * @see JFrame
 176       */
 177     public final static Integer FRAME_CONTENT_LAYER = new Integer(-30000);


 258      */
 259     public void removeAll() {
 260         Component[] children = getComponents();
 261         Hashtable<Component, Integer> cToL = getComponentToLayer();
 262         for (int counter = children.length - 1; counter >= 0; counter--) {
 263             Component c = children[counter];
 264             if (c != null && !(c instanceof JComponent)) {
 265                 cToL.remove(c);
 266             }
 267         }
 268         super.removeAll();
 269     }
 270 
 271     /**
 272      * Returns false if components in the pane can overlap, which makes
 273      * optimized drawing impossible. Otherwise, returns true.
 274      *
 275      * @return false if components can overlap, else true
 276      * @see JComponent#isOptimizedDrawingEnabled
 277      */

 278     public boolean isOptimizedDrawingEnabled() {
 279         return optimizedDrawingPossible;
 280     }
 281 
 282 
 283 //////////////////////////////////////////////////////////////////////////////
 284 //// New methods for managing layers
 285 //////////////////////////////////////////////////////////////////////////////
 286     /** Sets the layer property on a JComponent. This method does not cause
 287       * any side effects like setLayer() (painting, add/remove, etc).
 288       * Normally you should use the instance method setLayer(), in order to
 289       * get the desired side-effects (like repainting).
 290       *
 291       * @param c      the JComponent to move
 292       * @param layer  an int specifying the layer to move it to
 293       * @see #setLayer
 294       */
 295     public static void putLayer(JComponent c, int layer) {
 296         /// MAKE SURE THIS AND setLayer(Component c, int layer, int position)  are SYNCED
 297         c.putClientProperty(LAYER_PROPERTY, layer);


 721         String optimizedDrawingPossibleString = (optimizedDrawingPossible ?
 722                                                  "true" : "false");
 723 
 724         return super.paramString() +
 725         ",optimizedDrawingPossible=" + optimizedDrawingPossibleString;
 726     }
 727 
 728 /////////////////
 729 // Accessibility support
 730 ////////////////
 731 
 732     /**
 733      * Gets the AccessibleContext associated with this JLayeredPane.
 734      * For layered panes, the AccessibleContext takes the form of an
 735      * AccessibleJLayeredPane.
 736      * A new AccessibleJLayeredPane instance is created if necessary.
 737      *
 738      * @return an AccessibleJLayeredPane that serves as the
 739      *         AccessibleContext of this JLayeredPane
 740      */

 741     public AccessibleContext getAccessibleContext() {
 742         if (accessibleContext == null) {
 743             accessibleContext = new AccessibleJLayeredPane();
 744         }
 745         return accessibleContext;
 746     }
 747 
 748     /**
 749      * This class implements accessibility support for the
 750      * <code>JLayeredPane</code> class.  It provides an implementation of the
 751      * Java Accessibility API appropriate to layered pane user-interface
 752      * elements.
 753      * <p>
 754      * <strong>Warning:</strong>
 755      * Serialized objects of this class will not be compatible with
 756      * future Swing releases. The current serialization support is
 757      * appropriate for short term storage or RMI between applications running
 758      * the same version of Swing.  As of 1.4, support for long term storage
 759      * of all JavaBeans&trade;
 760      * has been added to the <code>java.beans</code> package.
   1 /*
   2  * Copyright (c) 1997, 2015, 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
  23  * questions.
  24  */
  25 package javax.swing;
  26 
  27 import java.awt.Component;
  28 import java.util.ArrayList;
  29 import java.util.Hashtable;
  30 import java.awt.Color;
  31 import java.awt.Graphics;
  32 import java.awt.Rectangle;
  33 import java.beans.JavaBean;
  34 import java.beans.BeanProperty;
  35 
  36 import sun.awt.SunToolkit;
  37 
  38 import javax.accessibility.*;
  39 
  40 /**
  41  * <code>JLayeredPane</code> adds depth to a JFC/Swing container,
  42  * allowing components to overlap each other when needed.
  43  * An <code>Integer</code> object specifies each component's depth in the
  44  * container, where higher-numbered components sit &quot;on top&quot; of other
  45  * components.
  46  * For task-oriented documentation and examples of using layered panes see
  47  * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html">How to Use a Layered Pane</a>,
  48  * a section in <em>The Java Tutorial</em>.
  49  *
  50  * <TABLE STYLE="FLOAT:RIGHT" BORDER="0" SUMMARY="layout">
  51  * <TR>
  52  *   <TD ALIGN="CENTER">
  53  *     <P STYLE="TEXT-ALIGN:CENTER"><IMG SRC="doc-files/JLayeredPane-1.gif"
  54  *     alt="The following text describes this image."
  55  *     WIDTH="269" HEIGHT="264" STYLE="FLOAT:BOTTOM; BORDER=0">


 141  * will affect all child components of this container without regard for
 142  * layer settings.
 143  * <p>
 144  * <strong>Warning:</strong> Swing is not thread safe. For more
 145  * information see <a
 146  * href="package-summary.html#threading">Swing's Threading
 147  * Policy</a>.
 148  * <p>
 149  * <strong>Warning:</strong>
 150  * Serialized objects of this class will not be compatible with
 151  * future Swing releases. The current serialization support is
 152  * appropriate for short term storage or RMI between applications running
 153  * the same version of Swing.  As of 1.4, support for long term storage
 154  * of all JavaBeans&trade;
 155  * has been added to the <code>java.beans</code> package.
 156  * Please see {@link java.beans.XMLEncoder}.
 157  *
 158  * @author David Kloba
 159  * @since 1.2
 160  */
 161 @JavaBean(defaultProperty = "accessibleContext")
 162 @SuppressWarnings("serial")
 163 public class JLayeredPane extends JComponent implements Accessible {
 164     /// Watch the values in getObjectForLayer()
 165     /** Convenience object defining the Default layer. Equivalent to new Integer(0).*/
 166     public final static Integer DEFAULT_LAYER = 0;
 167     /** Convenience object defining the Palette layer. Equivalent to new Integer(100).*/
 168     public final static Integer PALETTE_LAYER = 100;
 169     /** Convenience object defining the Modal layer. Equivalent to new Integer(200).*/
 170     public final static Integer MODAL_LAYER = 200;
 171     /** Convenience object defining the Popup layer. Equivalent to new Integer(300).*/
 172     public final static Integer POPUP_LAYER = 300;
 173     /** Convenience object defining the Drag layer. Equivalent to new Integer(400).*/
 174     public final static Integer DRAG_LAYER = 400;
 175     /** Convenience object defining the Frame Content layer.
 176       * This layer is normally only use to position the contentPane and menuBar
 177       * components of JFrame.
 178       * Equivalent to new Integer(-30000).
 179       * @see JFrame
 180       */
 181     public final static Integer FRAME_CONTENT_LAYER = new Integer(-30000);


 262      */
 263     public void removeAll() {
 264         Component[] children = getComponents();
 265         Hashtable<Component, Integer> cToL = getComponentToLayer();
 266         for (int counter = children.length - 1; counter >= 0; counter--) {
 267             Component c = children[counter];
 268             if (c != null && !(c instanceof JComponent)) {
 269                 cToL.remove(c);
 270             }
 271         }
 272         super.removeAll();
 273     }
 274 
 275     /**
 276      * Returns false if components in the pane can overlap, which makes
 277      * optimized drawing impossible. Otherwise, returns true.
 278      *
 279      * @return false if components can overlap, else true
 280      * @see JComponent#isOptimizedDrawingEnabled
 281      */
 282     @BeanProperty(bound = false)
 283     public boolean isOptimizedDrawingEnabled() {
 284         return optimizedDrawingPossible;
 285     }
 286 
 287 
 288 //////////////////////////////////////////////////////////////////////////////
 289 //// New methods for managing layers
 290 //////////////////////////////////////////////////////////////////////////////
 291     /** Sets the layer property on a JComponent. This method does not cause
 292       * any side effects like setLayer() (painting, add/remove, etc).
 293       * Normally you should use the instance method setLayer(), in order to
 294       * get the desired side-effects (like repainting).
 295       *
 296       * @param c      the JComponent to move
 297       * @param layer  an int specifying the layer to move it to
 298       * @see #setLayer
 299       */
 300     public static void putLayer(JComponent c, int layer) {
 301         /// MAKE SURE THIS AND setLayer(Component c, int layer, int position)  are SYNCED
 302         c.putClientProperty(LAYER_PROPERTY, layer);


 726         String optimizedDrawingPossibleString = (optimizedDrawingPossible ?
 727                                                  "true" : "false");
 728 
 729         return super.paramString() +
 730         ",optimizedDrawingPossible=" + optimizedDrawingPossibleString;
 731     }
 732 
 733 /////////////////
 734 // Accessibility support
 735 ////////////////
 736 
 737     /**
 738      * Gets the AccessibleContext associated with this JLayeredPane.
 739      * For layered panes, the AccessibleContext takes the form of an
 740      * AccessibleJLayeredPane.
 741      * A new AccessibleJLayeredPane instance is created if necessary.
 742      *
 743      * @return an AccessibleJLayeredPane that serves as the
 744      *         AccessibleContext of this JLayeredPane
 745      */
 746     @BeanProperty(bound = false)
 747     public AccessibleContext getAccessibleContext() {
 748         if (accessibleContext == null) {
 749             accessibleContext = new AccessibleJLayeredPane();
 750         }
 751         return accessibleContext;
 752     }
 753 
 754     /**
 755      * This class implements accessibility support for the
 756      * <code>JLayeredPane</code> class.  It provides an implementation of the
 757      * Java Accessibility API appropriate to layered pane user-interface
 758      * elements.
 759      * <p>
 760      * <strong>Warning:</strong>
 761      * Serialized objects of this class will not be compatible with
 762      * future Swing releases. The current serialization support is
 763      * appropriate for short term storage or RMI between applications running
 764      * the same version of Swing.  As of 1.4, support for long term storage
 765      * of all JavaBeans&trade;
 766      * has been added to the <code>java.beans</code> package.