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

Focus requests in a queue are removed while removing component

XMLWordPrintable

    • 01
    • x86
    • windows_nt



        Name: pa48320 Date: 11/29/2001

        (this bug is to track the JInitiator Replacement Project. please direct any
        questions to ###@###.###. thank you)

        There are certain actions which can potentially remove pending
        focus transfers from the KeyboardFocusManager?s list. The main
        example of that is removing a component from its container. This
        is a strange example, since why would one care whether a focus
        request was honored on a component we?re about to remove?

        Oracle has a requirement to adjust the Z-ordering of components
        within a container dynamically (it?s something our tool supports
        and of which Oracle Applications has made extensive use).
        Unfortunately, the JDK does not provide an API to modify the
        order of components within a container, and our Z-ordering
        support must be robust enough that we cannot accomplish it
        by merely hiding certain components. The only workaround
        we?ve found is to remove and re-add a set of components to
        the container in question.

        In some cases, we will request focus on a component whose
        canvas is about to be lowered within the Z-order. The component
        itself remains visible and is still a valid focus recipient,
        but in order to lower it, its canvas (i.e. its container) must
        be removed and re-added to the overall container. As noted above,
        this causes the focus request to be ignored, and in this
        situation the JDK will be justified in transferring focus to some
        arbitrary component since the application has not successfully
        managed focus on its own.
        (Review ID: 135396)
        ======================================================================

        customer supplies the following test case:

        // Testcase 3 (provided for Oracle bug 2112287)
        //
        // Removing and re-adding lightweight components to the same container
        // with a different index is a technique we use to achieve Z-ordering
        // support in the JDK.
        //
        // This SwingFocusTest testcase demonstrates a difference in behavior
        // between 1.1.8 and 1.4 in this regard. Namely, requesting focus on
        // a component before adjusting its Z-order in this way works properly
        // in JDK 1.1.8, but causes focus to be lost in JDK 1.4.
        //
        // Note that we use an Oracle lightweight component implementation
        // called EWT in our code rather than Swing, but for the purposes
        // of this testcase Swing demonstrates the same problem. This was
        // verified using Swing 1.1.1.
        //
        public class SwingFocusTestTwo extends JFrame implements ActionListener
        {
            JPanel panel = new JPanel();
            MyJButton button1 = new MyJButton("Push!");
            MyJTextField field1 = new MyJTextField(" Field 1");
            MyJTextField field2 = new MyJTextField(" Field 2");

            SwingFocusTestTwo()
            {
                Container contentPane = getContentPane();
                contentPane.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 )
                {
                    // We've requested focus on field2, possibly
                    // much earlier in our code.

                     field2.requestFocus();
                    //
                    // Then we need to raise field2 (or the canvas
                    // containing it) to the top of the Z-order,
                    // which entails removing and re-adding it to
                    // its container.

                    panel.remove(field2);
                    panel.add(field2, 1);
                    pack();
                }
            }

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

        class MyJTextField extends JTextField implements FocusListener
        {

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

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

            public void focusGained(FocusEvent e)
            {
                System.out.println("[MyJTextField] focusGained on " +

        ((MyJTextField)(e.getComponent())).getText());
            }

            public void focusLost(FocusEvent e)
            {
                System.out.println("[MyJTextField] focusLost on " +

        ((MyJTextField)(e.getComponent())).getText());
            }
        }

        class MyJButton extends JButton implements FocusListener
        {

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

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

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

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

              son Oleg Sukhodolsky (Inactive)
              pallenba Peter Allenbach (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: