JDK-8198809 : IllegalComponentStateException if Window did not have focus before clicking
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 8u151
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_8
  • CPU: x86
  • Submitted: 2018-02-22
  • Updated: 2019-08-02
  • Resolved: 2018-03-01
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)

ADDITIONAL OS VERSION INFORMATION :
Window 8.1 Pro
6.3.9600

A DESCRIPTION OF THE PROBLEM :
An IllegalComponentStateException is thrown if another Application had the focus and we directly click on an Component that switches a cardlayout.
This is 100% reproducable and is NOT AN DUPLICATE of JDK-8179665

REGRESSION.  Last worked in version 8u151

ADDITIONAL REGRESSION INFORMATION: 
Works in this Version:
Java(TM) SE Runtime Environment (build 1.8.0_91-b15)

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Run the Demo Code given below.
2. Activate any other Application. 
3. Klick on the Button "Page 2"

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The Page should be switched without throwing the Exception
ACTUAL -
The Page is switched, but an Exception is thrown.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
	at java.awt.Component.getLocationOnScreen_NoTreeLock(Component.java:2062)
	at java.awt.Component.getLocationOnScreen(Component.java:2036)
	at javax.swing.text.JTextComponent$InputMethodRequestsHandler.getTextLocation(JTextComponent.java:4643)
	at sun.awt.im.InputMethodContext.getTextLocation(InputMethodContext.java:278)
	at sun.awt.windows.WInputMethod$1.run(WInputMethod.java:588)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
	at java.awt.EventQueue.access$500(EventQueue.java:97)
	at java.awt.EventQueue$3.run(EventQueue.java:709)
	at java.awt.EventQueue$3.run(EventQueue.java:703)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
	at java.awt.EventQueue$4.run(EventQueue.java:733)
	at java.awt.EventQueue$4.run(EventQueue.java:731)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.IllegalComponentStateException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;



public class Frame 
{

   public static void main(String[] args) throws Exception
   {
      //Toolkit.getDefaultToolkit().getSystemEventQueue().push(new WorkaroundEventQueue());
      
      CardLayout cardLayout = new CardLayout();
      JPanel content = new JPanel();
      content.setLayout(cardLayout);

      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
      
      JPanel panel = new JPanel();
      panel.setLayout(new BorderLayout());
      panel.add(content, BorderLayout.CENTER);
      
      JPanel nav = new JPanel();
      panel.add(nav, BorderLayout.NORTH);
      JButton bt1 = new JButton("Page 1");
      nav.add(bt1);
      JButton bt2 = new JButton("Page 2");
      nav.add(bt2);
      bt1.addActionListener(new ActionListener()
      {
         @Override
         public void actionPerformed(ActionEvent e)
         {
            cardLayout.show(content, "page1");
         }
      });
      bt2.addActionListener(new ActionListener()
      {
         @Override
         public void actionPerformed(ActionEvent e)
         {
            cardLayout.show(content, "page2");
         }
      });
      bt1.setFocusable(false);
      bt2.setFocusable(false);

      JPanel page1 = new JPanel();
      content.add(page1,"page1");
      JTextField tf = new JTextField(10);
      page1.add(tf);

      JPanel page2 = new JPanel();
      content.add(page2,"page2");
      JTextField tf2 = new JTextField(20);
      page2.add(tf2);
      
      frame.setContentPane(panel);
      frame.pack();
      frame.setVisible(true);
      
   }
}

class WorkaroundEventQueue extends EventQueue
{
   /* (non-Javadoc)
    * @see java.awt.EventQueue#dispatchEvent(java.awt.AWTEvent)
    */
   @Override
   protected void dispatchEvent(AWTEvent event)
   {
      try
      {
         super.dispatchEvent(event);
      }
      catch(IllegalComponentStateException ex)
      {}
   }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Make the Buttons focusable again.
But this is not an Option in our real life Application, because we don't use buttons there.

Another Workaround would be to replace the EventQueue with the WorkaroundEventQueue by uncommenting the first line. This would catch the Exception and ignore it silently.


Comments
Submitter has confirmed back that the issue does not reproduce with JDK 8u172 when checked against the provided Test Case, and our real world application. Both of them work fine with the 172 ea Version. Therefore, closing this as duplicate of JDK-8179665 which has fix available with 8u172 ea build.
01-03-2018

An IllegalComponentStateException is thrown if another Application had the focus and we directly click on an Component that switches a cardlayout. Reported with: ============= JDK 8u151 Windows 8.1 Pro Checked this with reported versions 8u151 and 8u162 and could confirm the issue. However, it seems to have resolved in JDK 8u172 ea build with JDK-8179665. Results: ============== 8u162: Fail 8u172: OK 9.0.4: OK 10 ea b44: OK
28-02-2018