JDK-6469096 : requestFocusXXX on unfocused window causes incorrect results under X
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: linux
  • CPU: x86
  • Submitted: 2006-09-11
  • Updated: 2011-03-07
  • Resolved: 2011-03-07
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b14Fixed
Related Reports
Relates :  
Relates :  
Description
during investigation of 6458497 I have found that we have similar problems (in term of focus transfer) under X.  Here is a simple test to reproduce the problem.
You need to press "press me" button and transfer focus out of the java.

import java.awt.AWTEvent;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

/**
 *
 * @author son
 */
public class bug6458497 {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        JFrame jframe = new JFrame("test for 6458497") {
            public String toString() {
                return "frame1";
            }
        };
        jframe.setLayout(new FlowLayout());
        JButton jbtn1 = new JButton("press me") {
            public String toString() {
                return "button1";
            }
        };
        jframe.add(jbtn1);
        final JButton jbtn2 = new JButton("I'm going to have a focus") {
            public String toString() {
                return "button2";
            }
        };
        jframe.add(jbtn2);
        jframe.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        jframe.pack();

        jbtn1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                System.err.println("Got action, waiting...");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                System.err.println("requesting focus on jbtn2... " + jbtn2.requestFocus(false));
            }
        });
        jframe.setVisible(true);
        JFrame jframe2 = new JFrame("an opposite frame") {
            public String toString() {
                return "frame2";
            }
        };
        jframe2.add(new JButton("jbutton") {
            public String toString() {
                return "button3";
            }
        });
        jframe2.pack();
        jframe2.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        jframe2.setVisible(true);
    }
}

Comments
SUGGESTED FIX the webrev can be found at http://javaweb.sfbay/jcg/7/awt/6469096/ (also I've attached it to the CR)
02-05-2007

EVALUATION I decided to (re)set native focused every time we process native FocusIn/FocusOut on a toplevel (XWindowPeer.handleWindowFocus(In|Out)XXX methods) It looks like this approach works (at least based on test results :)
24-04-2007

EVALUATION Also I've found that we have similar (in term of focus) problem on X. There the cause of the problem is incorrect behavior of XKeyboardFocusManager.getCurrentFocusedWindow() and getCurrentFocusOwner() our code assumes that these methods return real native focused window/focus owner, but they return java focused window/focus owner. But on X, we can not return window which is natively focused at the moment, since it breaks focus transfer for window (at least if we click on window which owner is not active at this moment. Our code first sets focus on frame (its proxy) then sends WINDOW_GAINED_FOCUS to window, but request focus on component in this window fails because we return frame as focused window (at this place our code wants window :( annoying.
11-09-2006