Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5089436

REGRESSION: requestFocusInWindow() fails for comp. on JTabbedPane aftertab switch

XMLWordPrintable

    • b82
    • generic, x86
    • generic, solaris_2.5.1, windows_xp

        Name: rmT116609 Date: 08/19/2004


        FULL PRODUCT VERSION :
        java version "1.5.0-beta3"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta3-b60)
        Java HotSpot(TM) Client VM (build 1.5.0-beta3-b60, mixed mode, sharing)


        ADDITIONAL OS VERSION INFORMATION :
        Linux 2.6.4 #1 i686 Intel(R) Pentium(R) 4 CPU 2.40GHz GenuineIntel GNU/Linux

        Microsoft Windows XP [Version 6.1.2600]

        A DESCRIPTION OF THE PROBLEM :
        Calls to JComponent.requestFocusInWindow() return false and have no
        effect for components in a JTabbedPane when carried out straight after
        a tab switch.

        If carried out later (e.g during processing of a subsequent event) they
        work as expected.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Compile and execute the attached sample. Allow to run until
        "Adding change listener" is printed. Try switching between the
        two tabs using the mouse.

        The code illustrates four things at five second intervals:

        1) Selecting a tab and immediatly attempting to focus on a component
        2) Selecting a tab
        3) Focusing on a component on that tab
        4) Adding a ChangeListener to attempt to focus on the lower text field
        each time the user switches tabs
         
        All focus requests succeed with JDK 1.4.1/1.4.2 and all but (3) return false
        under JDK 1.5.0, build 60.


        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -

        Expected output would be as produced by version 1.4.1 (below).
        On clicking between the tabs at the end focus stays in the bottom
        two text fields.

        $ java TabbedPaneDemo
        Combined tab select and set focus
        Focusing on 22 - requestFocusInWindow() returned true
        Separate tab select
        Seperate focus adjustment
        Focusing on 12 - requestFocusInWindow() returned true
        Adding change listener
        Focusing on 22 - requestFocusInWindow() returned true
        Focusing on 12 - requestFocusInWindow() returned true
        Focusing on 22 - requestFocusInWindow() returned true
        Focusing on 12 - requestFocusInWindow() returned true

        ACTUAL -

        Output from 1.5.0 is as below. On clicking between the tabs
        at the end focus stays in the top two text fields.

        $ java TabbedPaneDemo
        Combined tab select and set focus
        Focusing on 22 - requestFocusInWindow() returned false
        Separate tab select
        Seperate focus adjustment
        Focusing on 12 - requestFocusInWindow() returned true
        Adding change listener
        Focusing on 22 - requestFocusInWindow() returned false
        Focusing on 12 - requestFocusInWindow() returned false
        Focusing on 22 - requestFocusInWindow() returned false
        Focusing on 12 - requestFocusInWindow() returned false


        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------

        /*
         * Based on TabbedPaneDemo.java from Sun Java tutorial
         */

        import java.awt.*;
        import javax.swing.*;
        import javax.swing.event.*;

        public class TabbedPaneDemo extends JPanel implements ChangeListener {

            JTextField textField11;
            JTextField textField12;
            JTextField textField21;
            JTextField textField22;
            JTabbedPane tabbedPane;

            public TabbedPaneDemo() {
                super(new GridLayout(1, 1));
                tabbedPane = new JTabbedPane();
                JComponent panel1 = makeTextPanel1();
                tabbedPane.addTab("Tab 1", null, panel1, "");
                JComponent panel2 = makeTextPanel2();
                tabbedPane.addTab("Tab 2", null, panel2, "");
                add(tabbedPane);

                actionThread.start();
            }

            protected JComponent makeTextPanel1() {
                JPanel panel = new JPanel(false);
                textField11 = new JTextField("11: test text one two three four");
                textField12 = new JTextField("12: example text");
                panel.setLayout(new GridLayout(2, 1));
                panel.add(textField11);
                panel.add(textField12);
                return panel;
            }

            protected JComponent makeTextPanel2() {
                JPanel panel = new JPanel(false);
                textField21 = new JTextField("21: example text");
                textField22 = new JTextField("22: example text");
                panel.setLayout(new GridLayout(2, 1));
                panel.add(textField21);
                panel.add(textField22);
                return panel;
            }

            private static void createAndShowGUI() {
                JFrame frame = new JFrame("TabbedPaneDemo");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().add(new TabbedPaneDemo(), BorderLayout.CENTER);
                frame.pack();
                frame.setVisible(true);
            }

            public static void main(String[] args) {
                javax.swing.SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        createAndShowGUI();
                    }
                });

            }

            public void stateChanged(ChangeEvent e) {
                attemptToSetFocus();
            }

            private void attemptToSetFocus() {
                boolean result;
                switch ( tabbedPane.getSelectedIndex() ) {
                case 0:
                    System.out.print("Focusing on 12 - ");
                    result = textField12.requestFocusInWindow();
                    break;
                case 1:
                    System.out.print("Focusing on 22 - ");
                    result = textField22.requestFocusInWindow();
                    break;
                default:
                    throw new Error("Unexpected index value");
                }

                System.out.println("requestFocusInWindow() returned " + result);
            }

            Thread actionThread = new Thread() {
                public void run() {
                    try { Thread.sleep(5000); } catch (InterruptedException e) {}

                    System.out.println("Combined tab select and set focus");

                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            tabbedPane.setSelectedIndex(1);
                            attemptToSetFocus();
                        }
                    });

                    try { Thread.sleep(5000); } catch (InterruptedException e) {}

                    System.out.println("Separate tab select");

                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            tabbedPane.setSelectedIndex(0);
                        }
                    });

                    try { Thread.sleep(5000); } catch (InterruptedException e) {}

                    System.out.println("Seperate focus adjustment");

                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            attemptToSetFocus();
                        }
                    });

                    try { Thread.sleep(5000); } catch (InterruptedException e) {}

                    System.out.println("Adding change listener");
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            tabbedPane.addChangeListener(TabbedPaneDemo.this);
                        }
                    });
                }
            };

        }


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

        CUSTOMER SUBMITTED WORKAROUND :

        Start a thread and which calls SwingUtilities.invokeLater()
        to call to requestFocusInWindow().

        Release Regression From : 1.4.2_05
        The above release value was the last known release where this
        bug was known to work. Since then there has been a regression.

        (Incident Review ID: 297637)
        ======================================================================

              shickeysunw Shannon Hickey (Inactive)
              rmandalasunw Ranjith Mandala (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: