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

Opposite component calculated inaccurately

XMLWordPrintable

    • hopper
    • x86
    • windows_2000
    • Verified



      Name: pzR10082 Date: 11/12/2001


      When a focus transfer occurs due to a focusable window made visible,
      the opposite component can be wrong. This happens because
      DefaultKeyboardFocusManager calls getMostRecentFocusOwner() on the
      window to determine the opposite component, and this call can return
      an invalid component. Here's a testcase that illustrates it:

      ///////////////////////////////////////////////////////
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      public class Killer implements ActionListener, FocusListener
      {
          JFrame mainFrame = new JFrame("Main");
          JFrame smallFrame = new JFrame("Small");
          JButton showButton = new JButton("Show Small Frame");
          JButton hideButton = new JButton("Hide This Frame");
          JLabel label = new JLabel("Nonfocusable Label");

          Killer() {
              showButton.addActionListener(this);
              showButton.addFocusListener(this);
              hideButton.addActionListener(this);

              mainFrame.getContentPane().add(showButton);
              smallFrame.getContentPane().add(hideButton);

              smallFrame.pack();
              smallFrame.setLocation(300,100);

              mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              mainFrame.pack();
              mainFrame.setVisible(true);
          }

          public static void main(String[] argv) {
              new Killer();
          }

          public void actionPerformed(ActionEvent ev) {
              JButton src = (JButton)ev.getSource();
              if (src == showButton) {
                  smallFrame.setVisible(true);
              } else if (src == hideButton) {
                  smallFrame.setVisible(false);
                  smallFrame.getContentPane().remove(hideButton);
                  smallFrame.getContentPane().add(label);
              }
          }

          public void focusLost(FocusEvent ev) {
              System.out.println("Focus lost by " + ev.getComponent());
              System.out.println("\tto " + ev.getOppositeComponent());
          }

          public void focusGained(FocusEvent ev) {
          }
      }
      ///////////////////////////////////////////////////////

      1. Press the "Show Small Frame" button. A frame appears. The button
      receives a FOCUS_LOST event, opposite component is "Hide This Frame"
      button in the new frame. This is correct.
      2. Press the Hide button, then press the Show button again. The button
      receives another FOCUS_LOST event, opposite component is the Hide
      button again. This is now wrong, because the Hide button has been
      removed from the frame, is not displayable and can't be focus owner.

      ======================================================================

            son Oleg Sukhodolsky (Inactive)
            peterz Peter Zhelezniakov
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: