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

getOppositeComponent() returns null from all Swing FOCUS_LOST events

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 Swing components, FOCUS_LOST and FOCUS_GAINED events are generated as expected. However, for all FOCUS_LOST events, the getOppositeComponent() method always returns null and it is not possible to determine the component that gained focus.

      This behavior (for Swing GUI Components) happens on all platforms:
      - Solaris, Solaris IA, Win98 and Linux

      This bug was found with new Focus Test Suite test:
      Swing/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.applet.*;
      import java.awt.event.*;
      import java.util.*;
      import javax.swing.*;

      public class OppositeComponentSample extends Frame implements FocusListener {

      JTextField textfield;
      JList list;
      JPanel testpanel;

      Component focusLost=null;
      Component focusGained=null;

      public OppositeComponentSample() {

      testpanel = new JPanel();
      textfield = new JTextField("TexField");
      textfield.setName("JTEXTFIELD");
      textfield.addFocusListener(this);

      String items[] = {"Item 1","Item 2","Item 3","Item 4","Item 5"};
      list = new JList(items);
      list.setName("JLIST");
      list.addFocusListener(this);

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

      add(testpanel);
      setSize(300,100);
      setVisible(true);
      }

      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[]) {
              
              OppositeComponentSample opc=new OppositeComponentSample();
          }
      }

            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: