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

Print this page




  40 import java.beans.VetoableChangeListener;
  41 import java.beans.VetoableChangeSupport;
  42 import java.beans.Transient;
  43 
  44 import java.applet.Applet;
  45 
  46 import java.io.Serializable;
  47 import java.io.ObjectOutputStream;
  48 import java.io.ObjectInputStream;
  49 import java.io.IOException;
  50 import java.io.ObjectInputValidation;
  51 import java.io.InvalidObjectException;
  52 import java.util.concurrent.atomic.AtomicBoolean;
  53 
  54 import javax.swing.border.*;
  55 import javax.swing.event.*;
  56 import javax.swing.plaf.*;
  57 import static javax.swing.ClientPropertyKey.*;
  58 import javax.accessibility.*;
  59 

  60 import sun.awt.SunToolkit;
  61 import sun.swing.SwingUtilities2;
  62 import sun.swing.UIClientPropertyKey;
  63 
  64 /**
  65  * The base class for all Swing components except top-level containers.
  66  * To use a component that inherits from <code>JComponent</code>,
  67  * you must place the component in a containment hierarchy
  68  * whose root is a top-level Swing container.
  69  * Top-level Swing containers --
  70  * such as <code>JFrame</code>, <code>JDialog</code>,
  71  * and <code>JApplet</code> --
  72  * are specialized components
  73  * that provide a place for other Swing components to paint themselves.
  74  * For an explanation of containment hierarchies, see
  75  * <a
  76  href="http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html">Swing Components and the Containment Hierarchy</a>,
  77  * a section in <em>The Java Tutorial</em>.
  78  *
  79  * <p>


4201         if (id == KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS) {
4202             setFlag(FOCUS_TRAVERSAL_KEYS_FORWARD_SET,true);
4203         } else if (id == KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS) {
4204             setFlag(FOCUS_TRAVERSAL_KEYS_BACKWARD_SET,true);
4205         }
4206         super.setFocusTraversalKeys(id,keystrokes);
4207     }
4208 
4209     /* --- Transitional java.awt.Component Support ---
4210      * The methods and fields in this section will migrate to
4211      * java.awt.Component in the next JDK release.
4212      */
4213 
4214     /**
4215      * Returns true if this component is lightweight, that is, if it doesn't
4216      * have a native window system peer.
4217      *
4218      * @param c  the {@code Component} to be checked
4219      * @return true if this component is lightweight
4220      */
4221     @SuppressWarnings("deprecation")
4222     public static boolean isLightweightComponent(Component c) {
4223         return c.getPeer() instanceof LightweightPeer;


4224     }
4225 
4226 
4227     /**
4228      * @deprecated As of JDK 5,
4229      * replaced by <code>Component.setBounds(int, int, int, int)</code>.
4230      * <p>
4231      * Moves and resizes this component.
4232      *
4233      * @param x  the new horizontal location
4234      * @param y  the new vertical location
4235      * @param w  the new width
4236      * @param h  the new height
4237      * @see java.awt.Component#setBounds
4238      */
4239     @Deprecated
4240     public void reshape(int x, int y, int w, int h) {
4241         super.reshape(x, y, w, h);
4242     }
4243 


5016         paintImmediately(r.x,r.y,r.width,r.height);
5017     }
5018 
5019     /**
5020      * Returns whether this component should be guaranteed to be on top.
5021      * For example, it would make no sense for <code>Menu</code>s to pop up
5022      * under another component, so they would always return true.
5023      * Most components will want to return false, hence that is the default.
5024      *
5025      * @return always returns false
5026      */
5027     // package private
5028     boolean alwaysOnTop() {
5029         return false;
5030     }
5031 
5032     void setPaintingChild(Component paintingChild) {
5033         this.paintingChild = paintingChild;
5034     }
5035 
5036     @SuppressWarnings("deprecation")
5037     void _paintImmediately(int x, int y, int w, int h) {
5038         Graphics g;
5039         Container c;
5040         Rectangle b;
5041 
5042         int tmpX, tmpY, tmpWidth, tmpHeight;
5043         int offsetX=0,offsetY=0;
5044 
5045         boolean hasBuffer = false;
5046 
5047         JComponent bufferedComponent = null;
5048         JComponent paintingComponent = this;
5049 
5050         RepaintManager repaintManager = RepaintManager.currentManager(this);
5051         // parent Container's up to Window or Applet. First container is
5052         // the direct parent. Note that in testing it was faster to
5053         // alloc a new Vector vs keeping a stack of them around, and gc
5054         // seemed to have a minimal effect on this.
5055         java.util.List<Component> path = new java.util.ArrayList<Component>(7);
5056         int pIndex = -1;


5142                                   jc.isDoubleBuffered()) {
5143                     hasBuffer = true;
5144                     bufferedComponent = jc;
5145                 }
5146 
5147                 // if we aren't on top, include the parent's clip
5148                 if (!ontop) {
5149                     int bx = c.getX();
5150                     int by = c.getY();
5151                     tmpWidth = c.getWidth();
5152                     tmpHeight = c.getHeight();
5153                     SwingUtilities.computeIntersection(tmpX,tmpY,tmpWidth,tmpHeight,paintImmediatelyClip);
5154                     paintImmediatelyClip.x += bx;
5155                     paintImmediatelyClip.y += by;
5156                     offsetX += bx;
5157                     offsetY += by;
5158                 }
5159         }
5160 
5161         // If the clip width or height is negative, don't bother painting
5162         if(c == null || c.getPeer() == null ||
5163                         paintImmediatelyClip.width <= 0 ||
5164                         paintImmediatelyClip.height <= 0) {
5165             recycleRectangle(paintImmediatelyClip);
5166             return;
5167         }
5168 
5169         paintingComponent.setFlag(IS_REPAINTING, true);
5170 
5171         paintImmediatelyClip.x -= offsetX;
5172         paintImmediatelyClip.y -= offsetY;
5173 
5174         // Notify the Components that are going to be painted of the
5175         // child component to paint to.
5176         if(paintingComponent != this) {
5177             Component comp;
5178             int i = pIndex;
5179             for(; i > 0 ; i--) {
5180                 comp = path.get(i);
5181                 if(comp instanceof JComponent) {
5182                     ((JComponent)comp).setPaintingChild(path.get(i-1));




  40 import java.beans.VetoableChangeListener;
  41 import java.beans.VetoableChangeSupport;
  42 import java.beans.Transient;
  43 
  44 import java.applet.Applet;
  45 
  46 import java.io.Serializable;
  47 import java.io.ObjectOutputStream;
  48 import java.io.ObjectInputStream;
  49 import java.io.IOException;
  50 import java.io.ObjectInputValidation;
  51 import java.io.InvalidObjectException;
  52 import java.util.concurrent.atomic.AtomicBoolean;
  53 
  54 import javax.swing.border.*;
  55 import javax.swing.event.*;
  56 import javax.swing.plaf.*;
  57 import static javax.swing.ClientPropertyKey.*;
  58 import javax.accessibility.*;
  59 
  60 import sun.awt.AWTAccessor;
  61 import sun.awt.SunToolkit;
  62 import sun.swing.SwingUtilities2;
  63 import sun.swing.UIClientPropertyKey;
  64 
  65 /**
  66  * The base class for all Swing components except top-level containers.
  67  * To use a component that inherits from <code>JComponent</code>,
  68  * you must place the component in a containment hierarchy
  69  * whose root is a top-level Swing container.
  70  * Top-level Swing containers --
  71  * such as <code>JFrame</code>, <code>JDialog</code>,
  72  * and <code>JApplet</code> --
  73  * are specialized components
  74  * that provide a place for other Swing components to paint themselves.
  75  * For an explanation of containment hierarchies, see
  76  * <a
  77  href="http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html">Swing Components and the Containment Hierarchy</a>,
  78  * a section in <em>The Java Tutorial</em>.
  79  *
  80  * <p>


4202         if (id == KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS) {
4203             setFlag(FOCUS_TRAVERSAL_KEYS_FORWARD_SET,true);
4204         } else if (id == KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS) {
4205             setFlag(FOCUS_TRAVERSAL_KEYS_BACKWARD_SET,true);
4206         }
4207         super.setFocusTraversalKeys(id,keystrokes);
4208     }
4209 
4210     /* --- Transitional java.awt.Component Support ---
4211      * The methods and fields in this section will migrate to
4212      * java.awt.Component in the next JDK release.
4213      */
4214 
4215     /**
4216      * Returns true if this component is lightweight, that is, if it doesn't
4217      * have a native window system peer.
4218      *
4219      * @param c  the {@code Component} to be checked
4220      * @return true if this component is lightweight
4221      */

4222     public static boolean isLightweightComponent(Component c) {
4223         // TODO we cannot call c.isLightweight() because it is incorrectly
4224         // overriden in DelegateContainer on osx.
4225         return AWTAccessor.getComponentAccessor().isLightweight(c);
4226     }
4227 
4228 
4229     /**
4230      * @deprecated As of JDK 5,
4231      * replaced by <code>Component.setBounds(int, int, int, int)</code>.
4232      * <p>
4233      * Moves and resizes this component.
4234      *
4235      * @param x  the new horizontal location
4236      * @param y  the new vertical location
4237      * @param w  the new width
4238      * @param h  the new height
4239      * @see java.awt.Component#setBounds
4240      */
4241     @Deprecated
4242     public void reshape(int x, int y, int w, int h) {
4243         super.reshape(x, y, w, h);
4244     }
4245 


5018         paintImmediately(r.x,r.y,r.width,r.height);
5019     }
5020 
5021     /**
5022      * Returns whether this component should be guaranteed to be on top.
5023      * For example, it would make no sense for <code>Menu</code>s to pop up
5024      * under another component, so they would always return true.
5025      * Most components will want to return false, hence that is the default.
5026      *
5027      * @return always returns false
5028      */
5029     // package private
5030     boolean alwaysOnTop() {
5031         return false;
5032     }
5033 
5034     void setPaintingChild(Component paintingChild) {
5035         this.paintingChild = paintingChild;
5036     }
5037 

5038     void _paintImmediately(int x, int y, int w, int h) {
5039         Graphics g;
5040         Container c;
5041         Rectangle b;
5042 
5043         int tmpX, tmpY, tmpWidth, tmpHeight;
5044         int offsetX=0,offsetY=0;
5045 
5046         boolean hasBuffer = false;
5047 
5048         JComponent bufferedComponent = null;
5049         JComponent paintingComponent = this;
5050 
5051         RepaintManager repaintManager = RepaintManager.currentManager(this);
5052         // parent Container's up to Window or Applet. First container is
5053         // the direct parent. Note that in testing it was faster to
5054         // alloc a new Vector vs keeping a stack of them around, and gc
5055         // seemed to have a minimal effect on this.
5056         java.util.List<Component> path = new java.util.ArrayList<Component>(7);
5057         int pIndex = -1;


5143                                   jc.isDoubleBuffered()) {
5144                     hasBuffer = true;
5145                     bufferedComponent = jc;
5146                 }
5147 
5148                 // if we aren't on top, include the parent's clip
5149                 if (!ontop) {
5150                     int bx = c.getX();
5151                     int by = c.getY();
5152                     tmpWidth = c.getWidth();
5153                     tmpHeight = c.getHeight();
5154                     SwingUtilities.computeIntersection(tmpX,tmpY,tmpWidth,tmpHeight,paintImmediatelyClip);
5155                     paintImmediatelyClip.x += bx;
5156                     paintImmediatelyClip.y += by;
5157                     offsetX += bx;
5158                     offsetY += by;
5159                 }
5160         }
5161 
5162         // If the clip width or height is negative, don't bother painting
5163         if(c == null || !c.isDisplayable() ||
5164                         paintImmediatelyClip.width <= 0 ||
5165                         paintImmediatelyClip.height <= 0) {
5166             recycleRectangle(paintImmediatelyClip);
5167             return;
5168         }
5169 
5170         paintingComponent.setFlag(IS_REPAINTING, true);
5171 
5172         paintImmediatelyClip.x -= offsetX;
5173         paintImmediatelyClip.y -= offsetY;
5174 
5175         // Notify the Components that are going to be painted of the
5176         // child component to paint to.
5177         if(paintingComponent != this) {
5178             Component comp;
5179             int i = pIndex;
5180             for(; i > 0 ; i--) {
5181                 comp = path.get(i);
5182                 if(comp instanceof JComponent) {
5183                     ((JComponent)comp).setPaintingChild(path.get(i-1));