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

Event.consume() doesn't work for key events on JText

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.2.0
    • 1.1.4
    • client-libs
    • swing1.0fcs
    • generic
    • generic



      Name: diC59631 Date: 01/14/98


      In the AWT it was possible to do 2 things with input key events
      that do not work in Swing 0.7 (or earlier versions):

      - consume an event. To restrict input to certain chars, one could
      do this in a KeyListener:

              public void keyPressed(KeyEvent e) {
                  // discard uppr case chars
                  if (Character.isUpperCase(e.getKeyChar()) {
                      e.consume();
                  }
              }

      - modify an event. In a KeyListener

              public void keyPressed(KeyEvent e) {
                  // map lower case chars to upper
                  if (Character.isLowerCase(e.getKeyChar()) {
                      e.setKeyChar(Character.toUpperCase(e.getKeyChar()));
                      e.setModifiers = Event.SHIFT_MASK;
                  }
              }

      These do not work for Swing - no changes or consumption occurs.
      I suspect that you are drawing the chars and *then* calling the
      listeners, rather than calling the listeners, checking event state,
      and then drawing.

      This works under the AWT:

      import java.awt.*;
      import java.awt.event.*;

      public class MapKey extends Frame {
       
         public static void main(String argv[]) {
              new MapKey().setVisible(true);
          }

          public MapKey() {
              TextField text = new TextField(20);
              add(text);
              pack();

              text.addKeyListener(new ToUpper());
          }
      }

      class ToUpper implements KeyListener {
          
          public void keyTyped(KeyEvent e) {
              // empty
          }

          public void keyPressed(KeyEvent e) {
              e.setModifiers(Event.SHIFT_MASK);
          }

          public void keyReleased(KeyEvent e) {
              // empty
          }
      }

      (or we could suppress an event by e.consume())

      Change the AWT components to Swing and it no longer works
      (Review ID: 23240)
      ======================================================================

            tprinzing Tim Prinzing (Inactive)
            dindrigo Daniel Indrigo (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: