src/share/classes/sun/awt/im/CompositionAreaHandler.java

Print this page

        

@@ -31,10 +31,11 @@
 import java.awt.event.InputMethodEvent;
 import java.awt.event.InputMethodListener;
 import java.awt.font.TextAttribute;
 import java.awt.font.TextHitInfo;
 import java.awt.im.InputMethodRequests;
+import java.lang.ref.WeakReference;
 import java.text.AttributedCharacterIterator;
 import java.text.AttributedCharacterIterator.Attribute;
 import java.text.AttributedString;
 
 /**

@@ -53,11 +54,11 @@
     private static Object compositionAreaLock = new Object();
     private static CompositionAreaHandler compositionAreaOwner; // synchronized through compositionArea
 
     private AttributedCharacterIterator composedText;
     private TextHitInfo caret = null;
-    private Component clientComponent = null;
+    private WeakReference<Component> clientComponent = new WeakReference<>(null);
     private InputMethodContext inputMethodContext;
 
     /**
      * Constructs the composition area handler.
      */

@@ -74,21 +75,22 @@
             if (compositionAreaOwner != null) {
                 compositionArea.setHandlerInfo(compositionAreaOwner, inputMethodContext);
             }
             // If the client component is an active client using below-the-spot style, then
             // make the composition window undecorated without a title bar.
-            if(clientComponent!=null){
-                InputMethodRequests req = clientComponent.getInputMethodRequests();
+            Component client = clientComponent.get();
+            if(client != null){
+                InputMethodRequests req = client.getInputMethodRequests();
                 if (req != null && inputMethodContext.useBelowTheSpotInput()) {
                     setCompositionAreaUndecorated(true);
                 }
             }
         }
     }
 
     void setClientComponent(Component clientComponent) {
-        this.clientComponent = clientComponent;
+        this.clientComponent = new WeakReference<>(clientComponent);
     }
 
     /**
      * Grabs the composition area, makes this handler its owner, and installs
      * the handler and its input context into the composition area for event

@@ -254,12 +256,13 @@
      * When using the composition window for an active client (below-the-spot
      * input), input method requests that do not relate to the display of
      * the composed text are forwarded to the client component.
      */
     InputMethodRequests getClientInputMethodRequests() {
-        if (clientComponent != null) {
-            return clientComponent.getInputMethodRequests();
+        Component client = clientComponent.get();
+        if (client != null) {
+            return client.getInputMethodRequests();
         }
 
         return null;
     }