8179665: [Windows] java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
authorssadetsky
Fri, 19 May 2017 07:06:28 -0700
changeset 45348 383dbc53e342
parent 45347 f85c1262be90
child 45349 b8f80bc16972
8179665: [Windows] java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location Reviewed-by: prr, serb
jdk/src/java.desktop/windows/classes/sun/awt/windows/WInputMethod.java
jdk/src/java.desktop/windows/native/libawt/windows/awt_Component.cpp
jdk/test/javax/swing/JFrame/AlwaysOnTop/AlwaysOnTopImeTest.java
--- a/jdk/src/java.desktop/windows/classes/sun/awt/windows/WInputMethod.java	Wed May 17 11:01:09 2017 -0700
+++ b/jdk/src/java.desktop/windows/classes/sun/awt/windows/WInputMethod.java	Fri May 19 07:06:28 2017 -0700
@@ -589,6 +589,9 @@
                 Component client = getClientComponent();
 
                 if (client != null) {
+                    if (!client.isShowing()) {
+                        return;
+                    }
                     if (haveActiveClient()) {
                             Rectangle rc = inputContext.getTextLocation(TextHitInfo.leading(0));
                             x = rc.x;
--- a/jdk/src/java.desktop/windows/native/libawt/windows/awt_Component.cpp	Wed May 17 11:01:09 2017 -0700
+++ b/jdk/src/java.desktop/windows/native/libawt/windows/awt_Component.cpp	Fri May 19 07:06:28 2017 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -3811,6 +3811,9 @@
     UINT bits = 1;
     POINT p = {0, 0}; // upper left corner of the client area
     HWND hWnd = GetHWnd();
+    if (!::IsWindowVisible(hWnd)) {
+        return;
+    }
     HWND hTop = GetTopLevelParentForWindow(hWnd);
     ::ClientToScreen(hTop, &p);
     if (!m_bitsCandType) {
@@ -4083,6 +4086,9 @@
 //
 void AwtComponent::InquireCandidatePosition()
 {
+    if (!::IsWindowVisible(GetHWnd())) {
+        return;
+    }
     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 
     // get global reference of WInputMethod class (run only once)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/JFrame/AlwaysOnTop/AlwaysOnTopImeTest.java	Fri May 19 07:06:28 2017 -0700
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug 8179665
+ * @summary [Windows] java.awt.IllegalComponentStateException: component must
+ *          be showing on the screen to determine its location
+ * @run main AlwaysOnTopImeTest
+ */
+
+import javax.swing.*;
+import java.awt.*;
+
+public class AlwaysOnTopImeTest {
+
+    private static JDialog d;
+    private static JFrame f;
+
+    public static void main(String[] args) throws Exception {
+        SwingUtilities.invokeLater(() -> {
+            f = new JFrame();
+            f.setVisible(true);
+            d = new JDialog(f);
+            d.add(new JTextField());
+            d.pack();
+            d.setModal(true);
+            f.setAlwaysOnTop(true);
+            d.setVisible(true);
+            f.setAlwaysOnTop(false);
+        });
+        Robot robot = new Robot();
+        robot.waitForIdle();
+        robot.delay(200);
+        SwingUtilities.invokeAndWait(() -> d.setVisible(false));
+        robot.waitForIdle();
+        robot.delay(200);
+        SwingUtilities.invokeLater(f::dispose);
+    }
+}