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

JInternalFrame KeyboardFocusManager listener mechanism ignores new KFMs

    XMLWordPrintable

Details

    Description

      FULL PRODUCT VERSION :
      Java(TM) SE Runtime Environment (build 1.6.0-rc-b95)

      A DESCRIPTION OF THE PROBLEM :
      The JInternalFrame staticly adds a listener to the KeyboardFocusManager:
          private static void addPropertyChangeListenerIfNecessary() {
              if (AppContext.getAppContext().get(PROPERTY_CHANGE_LISTENER_KEY) ==
                  null) {
                  PropertyChangeListener focusListener =
                      new FocusPropertyChangeListener();

                  AppContext.getAppContext().put(PROPERTY_CHANGE_LISTENER_KEY,
                      focusListener);

                  KeyboardFocusManager.getCurrentKeyboardFocusManager().
                      addPropertyChangeListener(focusListener);
              }
          }

          private static class FocusPropertyChangeListener implements
              PropertyChangeListener {
              public void propertyChange(PropertyChangeEvent e) {
                  if (e.getPropertyName() == "permanentFocusOwner") {
                      updateLastFocusOwner((Component)e.getNewValue());
                  }
              }
          }

      But does not take into account that the KeyboardFocusManager may change during runtime. This means that this mechanism is broken as soon as the developer sets a new KeyboardFocusManager.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run this test with assertions on. See that on the second run of the Timer, the assertion fails. It replicates what the JInternalFrame mechanism does by not reacting to changes in the KeyboardFocusManager:

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      the KeyboardFocusManager should be listened to, even after a change.
      ACTUAL -
      the official KeyboardFocusManager is not listened to.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
      import java.beans.*;

      public class TestKeyboardFocusManager implements Runnable, PropertyChangeListener, ActionListener{

          JFrame jf;
          public void run(){
              KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
              kfm.addPropertyChangeListener("permanentFocusOwner", this);
              jf = new JFrame();
              jf.setSize(300,300); jf.setVisible(true);
              jf.requestFocus();
              Timer t = new Timer(5000, this);
              t.start();
          }

          public void actionPerformed(ActionEvent e){
              System.out.println("Testing....");
              KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
              System.out.println("Permanent focus owner is: " + kfm.getPermanentFocusOwner().hashCode());
              assert(event != null): "Event is null";
      event = null;
      KeyboardFocusManager nkfm = new DefaultKeyboardFocusManager();
              KeyboardFocusManager.setCurrentKeyboardFocusManager(nkfm);
              jf.dispose();
              jf = new JFrame();
              jf.setSize(300,300);
              jf.setVisible(true);
              jf.requestFocus();
          }

          Object event;
          public void propertyChange(PropertyChangeEvent e){
      event = e;
              System.out.println(e);
          }


          public static void main(String ... args){
      SwingUtilities.invokeLater(new TestKeyboardFocusManager());
          }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      unsure if there is one, this is a private mechanism. I guess the user could replicate it and have the replication do the right thing.

      Attachments

        Activity

          People

            mbronsonsunw Mike Bronson (Inactive)
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Imported:
              Indexed: