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

Focus error when disabling in focus JTextField from TreeSelectionListener

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P2
    • Resolution: Fixed
    • Affects Version/s: 5.0
    • Fix Version/s: 6
    • Component/s: client-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b47
    • CPU:
      x86
    • OS:
      linux
    • Verification:
      Verified

      Backports

        Description

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

        ADDITIONAL OS VERSION INFORMATION :
        Linux gao-2004.weiqigao.com 2.4.22-1.2149.nptl #1 Wed Jan 7 12:57:33 EST 2004 i686 athlon i386 GNU/Linux

        A DESCRIPTION OF THE PROBLEM :
        On a panel with a JTree (tree), and two JTextFields (f1 and f2), if the TreeSelectionListener to tree is programmed to disable the text fields when a non-leaf node is selected, and enable the text fields when a leaf node is selected, then clicking on f1 and then a non-leaf node results in f2 getting the focus.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        1. Run java MyFrame
        2. Click on "Field 1"
        3. Click on "colors" tree node

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        [Tue Jan 27 21:49:51 CST 2004]: FOCUS_GAINED on tree
        [Tue Jan 27 21:49:55 CST 2004]: FOCUS_LOST on tree opposite f1
        [Tue Jan 27 21:49:55 CST 2004]: FOCUS_GAINED on f1 opposite tree
        [Tue Jan 27 21:49:57 CST 2004]: FOCUS_LOST on f1 opposite tree
        [Tue Jan 27 21:49:57 CST 2004]: FOCUS_GAINED on tree opposite f1

        tree has focus
        ACTUAL -
        [Tue Jan 27 21:49:51 CST 2004]: FOCUS_GAINED on tree
        [Tue Jan 27 21:49:55 CST 2004]: FOCUS_LOST on tree opposite f1
        [Tue Jan 27 21:49:55 CST 2004]: FOCUS_GAINED on f1 opposite tree
        [Tue Jan 27 21:49:57 CST 2004]: FOCUS_LOST on f1 opposite tree
        [Tue Jan 27 21:49:57 CST 2004]: FOCUS_GAINED on tree opposite f1
        [Tue Jan 27 21:49:57 CST 2004]: FOCUS_LOST on tree opposite f2
        [Tue Jan 27 21:49:57 CST 2004]: FOCUS_GAINED on f2 opposite tree

        tree does not have focus

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import javax.swing.*;
        import javax.swing.event.TreeSelectionEvent;
        import javax.swing.event.TreeSelectionListener;
        import javax.swing.tree.TreeNode;
        import java.awt.*;
        import java.awt.event.FocusEvent;
        import java.awt.event.FocusListener;
        import java.util.Date;

        public class MyFrame extends JFrame implements FocusListener, TreeSelectionListener {
            JTree tree = new JTree();
            JTextField f1 = new JTextField("Field 1", 20);
            JTextField f2 = new JTextField("Field 2", 20);
            JComponent[] fields = {f1, f2};

            public MyFrame() {
                super("MyFrame");
                layoutGui();
                hookupListeners();
                mainLoop();
            }

            private void layoutGui() {
                Container contentPane = getContentPane();

                contentPane.setLayout(new GridBagLayout());
                tree.expandRow(1);
                contentPane.add(tree, new GridBagConstraints(0, 0, 2, 1, 1.0, 0.0,
                        GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
                        new Insets(16, 16, 8, 16), 0, 0));
                contentPane.add(f1, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0,
                        GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
                        new Insets(8, 16, 8, 16), 0, 0));
                contentPane.add(f2, new GridBagConstraints(0, 2, 1, 1, 1.0, 0.0,
                        GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
                        new Insets(8, 16, 16, 16), 0, 0));
            }

            public void hookupListeners() {
                tree.addFocusListener(this);
                f1.addFocusListener(this);
                f2.addFocusListener(this);
                tree.addTreeSelectionListener(this);
            }

            public void mainLoop() {
                setDefaultCloseOperation(EXIT_ON_CLOSE);
                pack();
                setVisible(true);
            }

            public void focusGained(FocusEvent fe) {
                inspectFocusEvent(fe);
            }

            public void focusLost(FocusEvent fe) {
                inspectFocusEvent(fe);
            }

            private void inspectFocusEvent(FocusEvent fe) {
                StringBuffer buffer = new StringBuffer("[" + new Date().toString() + "]: ");

                String str = fe.getID() == FocusEvent.FOCUS_GAINED ? "FOCUS_GAINED on " :
                        fe.getID() == FocusEvent.FOCUS_LOST ? "FOCUS_LOST on " :
                        "";
                buffer.append(str).append(findComponentName(fe.getSource()));

                if (fe.getOppositeComponent() != null) {
                    buffer.append(" opposite " + findComponentName(fe.getOppositeComponent()));
                }

                System.out.println(buffer.toString());
            }

            private String findComponentName(Object component) {
                return component == f1 ? "f1 " :
                        component == f2 ? "f2 " :
                        component == tree ? "tree" :
                        "";
            }

            public void valueChanged(final TreeSelectionEvent e) {
                Object obj = e.getNewLeadSelectionPath().getLastPathComponent();
                if (obj instanceof TreeNode) {
                    for (int i = 0; i < fields.length; i++) {
                        fields[i].setEnabled(((TreeNode) obj).isLeaf());
                    }
                }
            }

            public static void main(String[] args) {
                new MyFrame();
            }
        }
        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Surround the content of the valueChanged() method with SwingUtilities.invokeLater().
        ###@###.### 10/16/04 18:45 GMT

          Attachments

            Issue Links

              Activity

                People

                Assignee:
                dmikhalksunw Denis Mikhalkin (Inactive)
                Reporter:
                gmanwanisunw Girish Manwani (Inactive)
                Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: