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

getOppositeComponent() returns null from all AWT FOCUS_GAINED and FOCUS_LOST eve

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P2 P2
    • 1.4.0
    • 1.4.0
    • client-libs
    • None
    • beta
    • generic
    • windows_98
    • Verified

      While switching focus between AWT components, FOCUS_LOST and FOCUS_GAINED events are generated as expected. However, for all FOCUS_GAINED and FOCUS_LOST events, the getOppositeComponent() method always returns null and it's not possible to determine the component that gained focus.

      This behavior (for AWT GUI Components) happens on following platforms:
      - Solaris, Solaris IA, and Linux (working OK for Win98)

      This bug was found with new Focus Test Suite test:
      AWT/OppositeComponentTest/

      Tested with build:
      - jdk1.4 Build40

      Sample code to reproduce bug (after GUI comes up just click on any of the components and observe the focus events generated):

      import java.awt.*;
      import java.awt.event.*;

      public class AwtOppositeComponent extends Frame implements FocusListener {

      Button button;
      TextField textfield;
      java.awt.List list;
      Panel testpanel;

      Component focusLost=null;
      Component focusGained=null;

      public AwtOppositeComponent() {

      testpanel = new Panel();

      button = new Button("Button");
      button.addFocusListener(this);

      textfield = new TextField("TextField");
      textfield.addFocusListener(this);

      list = new java.awt.List(2);
      list.add("Item 1");
      list.add("Item 2");
      list.add("Item 3");
      list.addFocusListener(this);

      testpanel.add(button);
      testpanel.add(textfield);
      testpanel.add(list);
      testpanel.setBackground(Color.yellow);

      add(testpanel);
      setSize(250,250);
      setVisible(true);
      show();
      }

      public void focusLost(FocusEvent fe) {
      System.out.println("\n** focusLost Method" );
      focusGained = fe.getOppositeComponent();
      if (focusGained == null) {
      System.out.println("** Warning: getOppositeComponent() is null");
      }
      System.out.println(fe);
      }

      public void focusGained(FocusEvent fe) {
                  System.out.println("\n** focusGained Method");
                  focusLost = fe.getOppositeComponent();
                  if (focusLost == null) {
      System.out.println("** Warning: getOppositeComponent() is null");
      }
                  System.out.println(fe);
      }

      public static void main(String args[]) {
      new AwtOppositeComponent();
          }
      }

            hgajewsksunw Hania Gajewska (Inactive)
            rreynagasunw Rick Reynaga (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: