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

TextField doesn't support AWT 1.0 inheritance event model (newEventsOnly==true).

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P5 P5
    • tbd
    • 7u6, 8, 9
    • client-libs
    • 7u7
    • os_x

      FULL PRODUCT VERSION :
      java version " 1.7.0_07 "
      Java(TM) SE Runtime Environment (build 1.7.0_07-b10)
      Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Mac OS X Mountain Lion - System Version: OS X 10.8.2 (12C60), Kernel Version: Darwin 12.2.0


      A DESCRIPTION OF THE PROBLEM :
      The TextField component in Oracle Java 7 on Mac OS X get marked with 'newEventsOnly' set to 'true', disabling any support for the deprecated AWT 1.0 inheritance event model.

      I think it happens in java.awt.Component with a call to this method:

          public synchronized void addInputMethodListener(InputMethodListener l) {
              if (l == null) {
                  return;
              }
              inputMethodListener = AWTEventMulticaster.add(inputMethodListener, l);
              newEventsOnly = true;
          }

      After that call, no events are reported with the AWT 1.0 event model, essentially disabling the component for any programs still using the old model.


      REGRESSION. Last worked in version 6u31

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the 'Java10' program included below with Oracle Java 7 on Mac OS X. Enter text in the text field and press the Enter key to post the text to the text area. Nothing happens. The Enter key events are never report to the program.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The Enter key should be reported as an action event to the program, allowing it to post the text to the text area. This is what happens with Java 7 on Linux and Windows.

      ACTUAL -
      Nothing happens. On Mac OS X you must press the Send button to post the text, as the Send button still reports the AWT 1.0 style events that the program expects.


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package com.status6.events;

      import java.awt.Button;
      import java.awt.Event;
      import java.awt.FlowLayout;
      import java.awt.Frame;
      import java.awt.Panel;
      import java.awt.TextArea;
      import java.awt.TextField;

      /**
       * A frame that uses the AWT 1.0 inheritance event model.
       *
       * @author John Neffenger
       */
      public class Java10 extends Frame {

          TextArea area;
          TextField field;
          Button send;
          Button quit;

          public Java10(String title) {
              super(title);
              area = new TextArea( " " , 25, 50, TextArea.SCROLLBARS_VERTICAL_ONLY);
              field = new TextField(50);
              send = new Button( " Send " );
              quit = new Button( " Quit " );
              area.setEditable(false);

              Panel panel = new Panel(new FlowLayout());
              panel.add(field);
              panel.add(send);
              panel.add(quit);

              add( " Center " , area);
              add( " South " , panel);
              pack();
          }

          private void postText() {
              area.append(field.getText() + '
      ');
              field.setText( " " );
              field.requestFocus();
          }

          private void quit() {
              setVisible(false);
              dispose();
              System.exit(0);
          }

          @Override
          public boolean action(Event event, Object what) {
              System.out.println( " Action = " + event);
              boolean handled = false;
              if (event.target == send || event.target == field) {
                  postText();
                  handled = true;
              } else if (event.target == quit) {
                  quit();
              }
              return handled;
          }

          @Override
          public boolean handleEvent(Event event) {
              if (event.id == Event.WINDOW_DESTROY) {
                  quit();
              }
              return super.handleEvent(event);
          }

          @Override
          public void requestFocus() {
              field.requestFocus();
          }

          /**
           * Creates the frame, sets it visible, and requests the keyboard focus.
           *
           * @param args the command line arguments
           */
          public static void main(String[] args) {
              Frame frame = new Java10( " My Frame " );
              frame.setVisible(true);
              frame.requestFocus();
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The workaround is to rewrite the application to use the AWT 1.1 delegation event model, as follows:

      package com.status6.events;

      import java.awt.Button;
      import java.awt.FlowLayout;
      import java.awt.Frame;
      import java.awt.Panel;
      import java.awt.TextArea;
      import java.awt.TextField;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.event.WindowAdapter;
      import java.awt.event.WindowEvent;

      /**
       * A frame that uses the AWT 1.1 delegation event model.
       *
       * @author John Neffenger
       */
      public class Java11 extends Frame {

          TextArea area;
          TextField field;
          Button send;
          Button quit;

          public Java11(String title) {
              super(title);
              area = new TextArea( " " , 25, 50, TextArea.SCROLLBARS_VERTICAL_ONLY);
              field = new TextField(50);
              send = new Button( " Send " );
              quit = new Button( " Quit " );
              area.setEditable(false);

              Panel panel = new Panel(new FlowLayout());
              panel.add(field);
              panel.add(send);
              panel.add(quit);

              add( " Center " , area);
              add( " South " , panel);
              addListeners();
              pack();
          }

          private void postText() {
              area.append(field.getText() + '
      ');
              field.setText( " " );
              field.requestFocus();
          }

          private void quit() {
              setVisible(false);
              dispose();
              System.exit(0);
          }

          private void addListeners() {
              // Using references to an anonymous inner class.
              ActionListener postingListener = new ActionListener() {
                  @Override
                  public void actionPerformed(ActionEvent e) {
                      System.out.println( " Posting text... " );
                      postText();
                  }
              };
              field.addActionListener(postingListener);
              send.addActionListener(postingListener);

              // Using the compact form of anonymous inner classes without references.
              quit.addActionListener(new ActionListener() {
                  @Override
                  public void actionPerformed(ActionEvent e) {
                      System.out.println( " Quitting... " );
                      quit();
                  }
              });
              addWindowListener(new WindowAdapter() {
                  @Override
                  public void windowClosing(WindowEvent e) {
                      System.out.println( " Window closing... " );
                      quit();
                  }
              });
          }

          @Override
          public void requestFocus() {
              field.requestFocus();
          }

          /**
           * Creates the frame, sets it visible, and requests the keyboard focus.
           *
           * @param args the command line arguments
           */
          public static void main(String[] args) {
              Frame frame = new Java11( " My Frame " );
              frame.setVisible(true);
              frame.requestFocus();
          }
      }

            serb Sergey Bylokhov
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: