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

Editing a JTable cell destroys FocusTraversalPolicy of parent container

    XMLWordPrintable

Details

    Description

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

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      If I create a custom FocusTraversalPolicy and set it on a container that is the FocusCycleRoot (Window, Dialog, etc.) and add a JTable as a child of the container, as soon as I edit the first cell, my custom FocusTraversalPolicy is overwritten by LegacyGlueFocusTraversalPolicy. It doesn't matter if it is a custom FTP or the default - it will be replaced.

      Related to LegacyGlueFocusTraversalPolicy.
      Could not find any existing, reported bugs with the same description.
      Exists in all JDK versions since 1.4 (LegacyGlueFocusTraversalPolicy).

      Cause:
      JTable.prepareEditor() calls jComp.setNextFocusableComponent(this).
      JTable.editCellAt() calls add(editorComp) which eventually calls JComponent.registerNextFocusableComponent(comp).
      This method ALWAYS replaces the nearest focusCycleRoot's FTP with LegacyGlueFocusTraversalPolicy when a next focusable component exists.

      I didn't check JTree or any other containers to see if they do the same thing.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a custom FocusTraversalPolicy and set it on a JFrame or JDialog. Add an editable JTable to it. Run the app. Edit a cell in the JTable. The FocusTraversalPolicy you originally added is now replaced by LegacyGlueFocusTraversalPolicy.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I would expect the original FocusTraversalPolicy to remain and not be replaced by LegacyGlueFocusTraversalPolicy for any reason.
      ACTUAL -
      As soon as I edit a cell in a JTable the parent container's FocusTraversalPolicy is replaced by LegacyGlueFocusTraversalPolicy and all of the behavior and state of the previous policy is lost.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Original FocusTraversalPolicy: javax.swing.LayoutFocusTraversalPolicy@105738

      Forced exception call stack when the FTP is replaced:

      java.lang.Exception
      at test.FocusTraversalPolicyTest$1.propertyChange(FocusTraversalPolicyTest.java:40)
      at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:330)
      at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:257)
      at java.awt.Component.firePropertyChange(Component.java:6695)
      at java.awt.Container.setFocusTraversalPolicy(Container.java:2525)
      at javax.swing.JComponent.registerNextFocusableComponent(JComponent.java:982)
      at javax.swing.JComponent.registerNextFocusableComponent(JComponent.java:968)
      at javax.swing.JComponent.addNotify(JComponent.java:4292)
      at java.awt.Container.addImpl(Container.java:658)
      at java.awt.Container.add(Container.java:307)
      at javax.swing.JTable.editCellAt(JTable.java:2537)
      at javax.swing.plaf.basic.BasicTableUI$MouseInputHandler.adjustFocusAndSelection(BasicTableUI.java:510)
      at javax.swing.plaf.basic.BasicTableUI$MouseInputHandler.mousePressed(BasicTableUI.java:494)
      at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:222)
      at java.awt.Component.processMouseEvent(Component.java:5097)
      at java.awt.Component.processEvent(Component.java:4897)
      at java.awt.Container.processEvent(Container.java:1569)
      at java.awt.Component.dispatchEventImpl(Component.java:3615)
      at java.awt.Container.dispatchEventImpl(Container.java:1627)
      at java.awt.Component.dispatchEvent(Component.java:3477)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
      at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3195)
      at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
      at java.awt.Container.dispatchEventImpl(Container.java:1613)
      at java.awt.Window.dispatchEventImpl(Window.java:1606)
      at java.awt.Component.dispatchEvent(Component.java:3477)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
      at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

      FocusTraversalPolicy after editing a cell in a JTable: javax.swing.LegacyGlueFocusTraversalPolicy@9f5011

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class FocusTraversalPolicyTest extends JFrame
      {
      public FocusTraversalPolicyTest()
      {
      setTitle("Focus Traversal Policy Test");

      Container p = getContentPane();
      p.setLayout(new BorderLayout());

      // Edit a cell in the table when you run the program
      JTable table = new JTable(5,5);
      p.add(table);

      System.out.println("Original FocusTraversalPolicy: "+FocusTraversalPolicyTest.this.getFocusTraversalPolicy());
      addPropertyChangeListener("focusTraversalPolicy", new PropertyChangeListener()
      {
      public void propertyChange(PropertyChangeEvent evt)
      {
      new Exception().printStackTrace();
      System.out.println("FocusTraversalPolicy after editing a cell in a JTable: "+FocusTraversalPolicyTest.this.getFocusTraversalPolicy());
      }
      });
      }


      public static void main(String[] args)
      {
      FocusTraversalPolicyTest test = new FocusTraversalPolicyTest();

      test.setSize(500,500);
      test.setLocation(100,100);
      test.setVisible(true);
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      I have written a temporary fix by overriding a method in JTable but I don't want to include it here because it is not something I want to have to maintain in my code into the future. It also prevents me from using JTable directly - I have to extend it.

      Attachments

        Activity

          People

            Unassigned Unassigned
            jssunw Jitender S (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: