< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1999, 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
  23  * questions.
  24  */
  25 
  26 package javax.swing;
  27 
  28 import java.awt.*;





  29 
  30 import sun.awt.ModalExclude;
  31 import sun.awt.SunToolkit;
  32 
  33 /**
  34  * Popups are used to display a <code>Component</code> to the user, typically
  35  * on top of all the other <code>Component</code>s in a particular containment
  36  * hierarchy. <code>Popup</code>s have a very small life cycle. Once you
  37  * have obtained a <code>Popup</code>, and hidden it (invoked the
  38  * <code>hide</code> method), you should no longer
  39  * invoke any methods on it. This allows the <code>PopupFactory</code> to cache
  40  * <code>Popup</code>s for later use.
  41  * <p>
  42  * The general contract is that if you need to change the size of the
  43  * <code>Component</code>, or location of the <code>Popup</code>, you should
  44  * obtain a new <code>Popup</code>.
  45  * <p>
  46  * <code>Popup</code> does not descend from <code>Component</code>, rather
  47  * implementations of <code>Popup</code> are responsible for creating
  48  * and maintaining their own <code>Component</code>s to render the
  49  * requested <code>Component</code> to the user.
  50  * <p>
  51  * You typically do not explicitly create an instance of <code>Popup</code>,


 140             component = null;
 141         }
 142         // If our parent is a DefaultFrame, we need to dispose it, too.
 143         if (window instanceof DefaultFrame) {
 144             window.dispose();
 145         }
 146     }
 147 
 148     /**
 149      * Resets the <code>Popup</code> to an initial state.
 150      */
 151     void reset(Component owner, Component contents, int ownerX, int ownerY) {
 152         if (getComponent() == null) {
 153             component = createComponent(owner);
 154         }
 155 
 156         Component c = getComponent();
 157 
 158         if (c instanceof JWindow) {
 159             JWindow component = (JWindow)getComponent();
 160 
 161             component.setLocation(ownerX, ownerY);
 162             component.getContentPane().add(contents, BorderLayout.CENTER);
 163             component.invalidate();
 164             component.validate();
 165             if(component.isVisible()) {
 166                 // Do not call pack() if window is not visible to
 167                 // avoid early native peer creation
 168                 pack();
 169             }
 170         }
 171     }
 172 
 173 
 174     /**
 175      * Causes the <code>Popup</code> to be sized to fit the preferred size
 176      * of the <code>Component</code> it contains.
 177      */
 178     void pack() {
 179         Component component = getComponent();
 180 
 181         if (component instanceof Window) {


   1 /*
   2  * Copyright (c) 1999, 2020, 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 
  26 package javax.swing;
  27 
  28 import java.awt.BorderLayout;
  29 import java.awt.Component;
  30 import java.awt.Frame;
  31 import java.awt.Graphics;
  32 import java.awt.GraphicsEnvironment;
  33 import java.awt.Window;
  34 
  35 import sun.awt.ModalExclude;

  36 
  37 /**
  38  * Popups are used to display a <code>Component</code> to the user, typically
  39  * on top of all the other <code>Component</code>s in a particular containment
  40  * hierarchy. <code>Popup</code>s have a very small life cycle. Once you
  41  * have obtained a <code>Popup</code>, and hidden it (invoked the
  42  * <code>hide</code> method), you should no longer
  43  * invoke any methods on it. This allows the <code>PopupFactory</code> to cache
  44  * <code>Popup</code>s for later use.
  45  * <p>
  46  * The general contract is that if you need to change the size of the
  47  * <code>Component</code>, or location of the <code>Popup</code>, you should
  48  * obtain a new <code>Popup</code>.
  49  * <p>
  50  * <code>Popup</code> does not descend from <code>Component</code>, rather
  51  * implementations of <code>Popup</code> are responsible for creating
  52  * and maintaining their own <code>Component</code>s to render the
  53  * requested <code>Component</code> to the user.
  54  * <p>
  55  * You typically do not explicitly create an instance of <code>Popup</code>,


 144             component = null;
 145         }
 146         // If our parent is a DefaultFrame, we need to dispose it, too.
 147         if (window instanceof DefaultFrame) {
 148             window.dispose();
 149         }
 150     }
 151 
 152     /**
 153      * Resets the <code>Popup</code> to an initial state.
 154      */
 155     void reset(Component owner, Component contents, int ownerX, int ownerY) {
 156         if (getComponent() == null) {
 157             component = createComponent(owner);
 158         }
 159 
 160         Component c = getComponent();
 161 
 162         if (c instanceof JWindow) {
 163             JWindow component = (JWindow)getComponent();
 164             // Sets the proper location, and resets internal state of the window
 165             component.setBounds(ownerX, ownerY, 1, 1);
 166             component.getContentPane().add(contents, BorderLayout.CENTER);
 167             component.invalidate();
 168             component.validate();
 169             if(component.isVisible()) {
 170                 // Do not call pack() if window is not visible to
 171                 // avoid early native peer creation
 172                 pack();
 173             }
 174         }
 175     }
 176 
 177 
 178     /**
 179      * Causes the <code>Popup</code> to be sized to fit the preferred size
 180      * of the <code>Component</code> it contains.
 181      */
 182     void pack() {
 183         Component component = getComponent();
 184 
 185         if (component instanceof Window) {


< prev index next >