United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6448190 : Focus issue with JFrame and JButton

Details
Type:
Bug
Submit Date:
2006-07-12
Status:
Resolved
Updated Date:
2011-02-16
Project Name:
JDK
Resolved Date:
2006-08-19
Component:
client-libs
OS:
windows_xp,windows
Sub-Component:
java.awt
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
1.4.2_12,5.0u6
Fixed Versions:
1.4.2_13 (b03)

Related Reports
Backport:
Backport:
Backport:
Duplicate:
Relates:

Sub Tasks

Description
while running the below testcase focus and functionality are lost after
moving focus out of IE and back to the applet.

Steps to reproduce.

Click serveral(4-6) times on the Non navigable Button
move the cursor out of IE and click on another window.
In my test I used a command prompt
move cursor back into the applet and click on the Focuesed Button
and then on the Non navigable Button.
Focus should be lost on the the Focused Button
and functionality lost on both buttons.
I've run it a few times and sometimes you may need to repeat the above
a couple of times.

(edited and replaced in this description the correct test case)
NewMDIDemo.java

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;

import sun.awt.SunToolkit;


public class NewMDIDemo extends JApplet
{
  public void init()
  {
    JPanel comp = new JPanel();
    _createDummy(comp);
    getContentPane().add("Center", comp);

    Component parent = comp.getParent();
    while ( parent != null && !(parent instanceof Frame) )
    {
        parent = parent.getParent();
    }
    SunToolkit.setLWRequestStatus((Window)parent, true);
  }

  private void _createDummy(JPanel rootPanel)
  {
    rootPanel.setLayout(new BorderLayout());
    final JButton btn1 = new JButton("Non navigable Button");
    final JButton btn2 = new JButton("Focused Button");
    rootPanel.add(btn1, BorderLayout.NORTH);
    rootPanel.add(btn2);

    btn1.addFocusListener(new FocusAdapter()
    {
      public void focusGained(FocusEvent event)
      {
        try
        {
          synchronized (this)
          {
            wait(3000);
          }
        }
        catch (Exception ex)
        {
          System.out.println(ex.toString());
        }

        btn2.requestFocus();
      }
    });

    btn2.addFocusListener(new FocusAdapter()
    {
      public void focusGained(FocusEvent event)
      {
        System.out.println("btn2 focusGained");
      }
    });
  }
}
revised/corrected test

                                    

Comments
EVALUATION

perhaps we should not allow synchronous focus request if we are in processing of
pending lightweigh requests (i.e. KFM.clearingCurrentLightweightRequests is true)
                                     
2006-07-19
EVALUATION

I'm able to reproduce the bug with Mustang-beta2 + IE + WinXP SP1. Focus is sometimes lost and after some random clicks is restored. I have also noticed an exception in Java Console:

Exception in thread "AWT-EventQueue-1" java.lang.SecurityException: this KeyboardFocusManager is not installed in the current thread's context
	at java.awt.KeyboardFocusManager.getGlobalFocusOwner(KeyboardFocusManager.java:458)
	at java.awt.KeyboardFocusManager.processSynchronousLightweightTransfer(KeyboardFocusManager.java:2170)
	at sun.awt.windows.WComponentPeer.processSynchronousLightweightTransfer(Native Method)
	at sun.awt.windows.WComponentPeer.requestFocus(WComponentPeer.java:539)
	at java.awt.Component.requestFocusHelper(Component.java:7094)
	at java.awt.Component.requestFocusHelper(Component.java:7050)
	at java.awt.Component.requestFocus(Component.java:6870)
	at javax.swing.JComponent.requestFocus(JComponent.java:1461)
	at NewMDIDemo$1.focusGained(NewMDIDemo.java:57)
	at java.awt.AWTEventMulticaster.focusGained(AWTEventMulticaster.java:162)
	at java.awt.Component.processFocusEvent(Component.java:5868)
	at java.awt.Component.processEvent(Component.java:5735)
	at java.awt.Container.processEvent(Container.java:1984)
	at java.awt.Component.dispatchEventImpl(Component.java:4387)
	at java.awt.Container.dispatchEventImpl(Container.java:2042)
	at java.awt.Component.dispatchEvent(Component.java:4217)
	at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1828)
	at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:863)
	at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:501)
	at java.awt.Component.dispatchEventImpl(Component.java:4259)
	at java.awt.Container.dispatchEventImpl(Container.java:2042)
	at java.awt.Component.dispatchEvent(Component.java:4217)
	at java.awt.KeyboardFocusManager.processCurrentLightweightRequests(KeyboardFocusManager.java:2545)
	at java.awt.KeyboardFocusManager.retargetFocusEvent(KeyboardFocusManager.java:2791)
	at java.awt.Component.dispatchEventImpl(Component.java:4252)
	at java.awt.Container.dispatchEventImpl(Container.java:2042)
	at java.awt.Component.dispatchEvent(Component.java:4217)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

I'm not sure if it is related to focus troubles as this exception occured after focus-related problems.
                                     
2006-07-13



Hardware and Software, Engineered to Work Together