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

Swing text components don't handle KEY_TYPED event with Backspace character

XMLWordPrintable

    • kestrel
    • x86
    • windows_95, windows_nt

      The Swing text components don't handle KEY_TYPED events with the Backspace character; apparently they listen to the corresponding KEY_PRESSED events instead. That is a problem for some Windows input methods, which implement an "Undo Commit" feature by sending WM_CHAR messages with the Backspace character to delete recently committed text. These messages do produce the desired result in AWT text components, as the following test case shows:

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

      public class SwingBackspace extends Applet {

          private static TextArea awtTextArea;
          private static JTextArea swingTextArea;

          public void init() {
              setLayout(new GridLayout(2, 1, 10, 10));
              awtTextArea = new TextArea();
              swingTextArea = new JTextArea();
              add(awtTextArea);
              add(swingTextArea);
          }
          
          private void postEvents(Component target, String text) {
              EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
              for (int i = 0; i < text.length(); i++) {
                  char ch = text.charAt(i);
                  queue.postEvent(new KeyEvent(target, KeyEvent.KEY_TYPED, 0, 0, KeyEvent.VK_UNDEFINED, ch));
              }
          }

          public void start() {
              postEvents(awtTextArea, "AWT - hello, bug\u0008\u0008\u0008\u0008\u0008!");
              postEvents(swingTextArea, "Swing - hello, bug\u0008\u0008\u0008\u0008\u0008!");
          }
          
          public void stop() {
          }

          public static void main(String argv[]) {
              SwingBackspace test = new SwingBackspace();
              test.init();
              
              Frame frame = new Frame("SwingBackspace Test");
              frame.pack(); // adds peer
              frame.add("Center", test);
              frame.setSize(400, 400);
              WindowListener listener = new WindowAdapter() {
                      public void windowClosing(WindowEvent e) {
                          e.getWindow().dispose();
                          System.exit(0);
                      }
                  };
              frame.addWindowListener(listener);
              frame.pack();
              frame.show();
              
              swingTextArea.requestFocus();

              test.start();
          }
          
      }

            tprinzing Tim Prinzing (Inactive)
            nlindenbsunw Norbert Lindenberg (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: