JDK-6274573 : REGRESSION: Focus does not go to items present on invisible containers
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 5.0u3
  • Priority: P2
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic
  • CPU: generic
  • Submitted: 2005-05-23
  • Updated: 2010-04-02
  • Resolved: 2005-07-22
Related Reports
Relates :  
Description
If focus requested on an item that is placed on a container that is not visible, then focus does not travel to that item. This worked fine in JDK 1.4, but does not work with JDK 1.5. I guess this bug is similar to the bug 5089436 that has been filed.

This has been broken in JDK 1.5 due to the lines 6549 to 6543 of java.awt.Component. The lines are :

                if (recursivelyInvisible) {
                    focusLog.finest("FAIL 1.5");
                    return false;
                }

Testcase
---------
import java.awt.*;
import java.awt.event.*;

public class FocusTest extends Frame implements ActionListener
{
    Panel panel = new Panel();
    MyButton button1 = new MyButton("Push!");
    MyTextField field1 = new MyTextField(" Field 1");
    MyTextField field2 = new MyTextField(" Field 2");

    FocusTest()
    {
        add(panel);
        addWindowListener(
        new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
        System.exit(0); } } );
        button1.addActionListener(this);
        panel.add(button1);
        panel.add(field1);
        panel.add(field2);
        pack();
    }

    public void actionPerformed(ActionEvent event)
    {
        if ( event.getSource() == button1 )
        {
            panel.setVisible(false);
            field2.requestFocus();
            panel.setVisible(true);
        }
    }

    static public void main(String[] args)
    {
        FocusTest ft = new FocusTest();
        ft.setVisible(true);
    }
}

class MyTextField extends TextField implements FocusListener
{

    MyTextField(String s)
    {
        super(s);
        addFocusListener(this);
    }

    public void requestFocus()
    {
        System.out.println("[MyTextField] requestFocus() from "+
        this.getText());
        super.requestFocus();
    }

    public void focusGained(FocusEvent e)
    {
        System.out.println("[MyTextField] focusGained on " +((MyTextField)(e.getComponent())).getText());
    }

    public void focusLost(FocusEvent e)
    {
        System.out.println("[MyTextField] focusLost on " +((MyTextField)(e.getComponent())).getText());
    }
}

class MyButton extends Button implements FocusListener
{
    MyButton(String s)
    {
        super(s);
        addFocusListener(this);
    }

    public void requestFocus()
    {
        System.out.println("[MyButton] requestFocus() from " + this.getLabel());
        super.requestFocus();
    }

    public void focusGained(FocusEvent e)
    {
        System.out.println("[MyButton] focusGained on " + ((MyButton)(e.getComponent())).getLabel());
    }

    public void focusLost(FocusEvent e)
    {
        System.out.println("[MyButton] focusLost on " + ((MyButton)(e.getComponent())).getLabel());
    }
}

Compile and run the testcase. The window that comes up,shows three items - a push button and two text fields.

Behaviour in JDK1.4
--------------------
1. When the push button is pressed,the focus comes to the text field named "Field2"
2. The following are shown on the console :
[MyButton] focusGained on Push!
[MyTextField] requestFocus() from  Field 2
[MyButton] focusLost on Push!
[MyTextField] focusGained on  Field 2

Behaviour in JDK 1.5
-------------------------
1. When the push button is pressed,the focus does not comes to the text field named "Field2"
2. The following are shown on the console :
[MyButton] focusGained on Push!
[MyTextField] requestFocus() from  Field 2
[MyButton] focusLost on Push!

Release Regression From : 1.4.2_08
The above release value was the last known release where this 
bug was known to work. Since then there has been a regression.
###@###.### 2005-05-23 16:20:03 GMT

Comments
EVALUATION This is a documented behavior: public void requestFocus() Requests that this Component get the input focus, and that this Component's top-level ancestor become the focused Window. *****This component must be displayable, focusable, visible and all of its ancestors (with the exception of the top-level Window) must be visible for the request to be granted. ***** There are platform limitations which do not allow us to transfer focus to invislbe components. We can not change this behavior so I am closing this bug.
22-07-2005