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

Component.requestFocus() fails for components on JWindow.

    XMLWordPrintable

Details

    • 02
    • generic
    • generic
    • Verified

    Backports

      Description



        Name: krT82822 Date: 12/22/98


        Focus management appears to be handled differently in JWindow and JFrame as the following example shows. The sample code attempts to
        create a JFrame with a JButton that, when pressed, creates a JWindow with a JTextField. It then attempts to give the JTextField focus immediately
        after it is displayed.

        In practice, the field never receives focus. Trying to set focus to the window yields interesting, but unhelpful results (the field gets and loses focus
        before the window does.) Setting focus on a timer doesn't do the trick. I also have a nasty workaround that works on Win32 but not Solaris, but that
        really isn't any better.

        The attempt to transfer focus works flawlessly if the field is placed in a JFrame as implemented using the commented out code in the sample below,
        so it seems clear that JWindow is somehow failing to provide proper focus management.

          import java.awt.event.*;
          import javax.swing.*;
          public class Test {
            public static void main(String args[]) {
              JFrame frame = new JFrame();
              JButton button = new JButton("Test");
              frame.getContentPane().add(button);
              frame.pack();

              frame.addFocusListener(new NoisyFocusListener("Frame"));
              button.addFocusListener(new NoisyFocusListener("Button"));

              button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                  JWindow window = new JWindow();
          // JFrame window = new JFrame();
                  JTextField field = new JTextField("Edit Me!");
                  window.getContentPane().add(field);
                  window.pack();

                  field.addFocusListener(new NoisyFocusListener("Field"));
                  window.addFocusListener(new NoisyFocusListener("Window"));

                  window.setVisible(true);
                  field.requestFocus();
                }
              });
              frame.setVisible(true);
            }

            static class NoisyFocusListener implements FocusListener {
              private String name;
              public NoisyFocusListener(String name) {
                this.name = name;
              }
              public void focusGained(FocusEvent e) {
                System.out.println(name + (e.isTemporary() ? " got temporary focus." : " got focus."));
              }
              public void focusLost(FocusEvent e) {
                System.out.println(name + (e.isTemporary() ? " lost temporary focus." : " lost focus."));
              }
            }
          }
        (Review ID: 48582)
        ======================================================================

        This bug turns out to be a show stopper for a shipping SAP application.
        They worked around it by shipping their application on 1.1.6.
        They run in other problems now which are fixed in higher releases!
        This bug is now a show stopper for a shipping, revenue generating
        commercial Java application (Hotel management).

        Merlin beta is far out and unacceaptable

        Regards

        Stefan Schneider


        stefan.schneider@eng 1999-11-12

        This is also causing an accessibility problem for JavaHelp V1.1 popups in the ContentViewer. We cannot adequately test the code to verify a correct fix for setting focus to the popup.

        Attachments

          Issue Links

            Activity

              People

                dmendenhsunw David Mendenhall (Inactive)
                kryansunw Kevin Ryan (Inactive)
                Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: