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

Regression: Kestrel RC1 Regression: JTextField/Alt+Backspace problem

XMLWordPrintable

    • merlin
    • x86
    • windows_98, windows_nt



      Name: skT88420 Date: 02/01/2000


      java version "1.3.0rc1"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0rc1-T)
      Java HotSpot(TM) Client VM (build 1.3.0rc1-S, mixed mode)

      As I understand it, the Alt key used in combination
      with another key offers an "alternative layer" of
      functions. Pressing such a combination should
      usually not result in the "regular" action associated
      with the specific key being performed, instead an
      "alternative" (system or user defined) action should
      be taken, if there is any.

      The point in case is the effect of hitting Alt+Backspace
      while a JTextField has the focus. In one of my applications,
      there is an action registered with this specific event
      and when it is triggered, it is supposed to perform
      a meaningful operation - in the test case, this has
      been reduced to printing a simple message. In 1.2.2,
      this worked as expected. In 1.3.0RC, however, in addition
      to invoking the action, the last character before the caret
      position is deleted, as if the user had pressed Backspace
      without any modifier key. This is a regression.

      Alt+Backspace should not behave like regular Backspace,
      and it should even less do so when the user has supplied
      a custom action for that event. Maybe other key combinations
      are affected too, but this is the one that caught my eye.

      --

      Test case (using 1.3 API):

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

      public class BackspaceBug extends JDialog {
        public BackspaceBug() {
          JPanel pnl = new JPanel();
          pnl.setLayout(new GridBagLayout());
          
            JTextField tf = new JTextField(24);
            tf.getInputMap(tf.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(
              KeyEvent.VK_BACK_SPACE, KeyEvent.ALT_MASK), "test");
            tf.getActionMap().put("test", new AbstractAction() {
                public void actionPerformed(ActionEvent ev) {
                  System.out.println("Alt+Backspace action performed.");
                }
              });
            tf.setText("Press Alt+Backspace");
            tf.setCaretPosition(tf.getText().length());
            
            pnl.add(tf, new GridBagConstraints(
              0, GridBagConstraints.RELATIVE, 1, 1, 0, 0,
              GridBagConstraints.CENTER, GridBagConstraints.NONE,
              new Insets(12, 12, 12, 12), 0, 0));
            
          setContentPane(pnl);
          
          pack();
          setLocation((getToolkit().getScreenSize().width - getWidth()) / 2,
            ((getToolkit().getScreenSize().height) - getHeight()) / 2);
            
          setVisible(true);
        }
        
        public static void main(String[] args) {
          new BackspaceBug();
        }
      }
      (Review ID: 100607)
      ======================================================================

      Name: skT88420 Date: 02/03/2000


      java version "1.3.0rc1"

      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0rc1-T)
      Java HotSpot(TM) Client VM (build 1.3.0rc1-S, mixed mode)

      I restrict the amount of characters to typed in certain text fields by adding a
      KeyListener. If the user types a key and the field is already full, I consume
      the event.

      This works great in 1.2.2, but in 1.3 the backspace key does not work if the
      field is full. The event is being consumed in the keyTyped method.

      In 1.2.2, backspace and delete are handled in the keyPressed methods. This
      being the case, consuming the keyTyped event makes no difference and the
      backspace works when the field is full.

        To use the example below, type in 10 characters. After that, all keyTyped
      events will be consumed. The backspace is rendered useless. By using the
      arrow keys (or the mouse) place the cursor in the middle of the text and hit
      the delete key. This *will* correctly delete a character. Everything will
      work fine until the field fills up again.

      As a workaround I tried moving the code that consumes the events to the
      keyPressed method. This does not work. It seems that consuming a keyPress has
      no bearing on whether it is still be "typed". Is this correct?


      CODE:
      =====

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

      public final class KeyEventBug extends JFrame {
      public final static int LIMIT = 10;

      private JTextField field, pressed, typed, released;

      public static void main(String argv[]) {
      KeyEventBug app = new KeyEventBug();
      }
      public KeyEventBug() {
      super("Key Event Bug");
      JPanel panel = new JPanel();
      panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));

      pressed = new JTextField(15);
      typed = new JTextField(15);
      released = new JTextField(15);

      field = new JTextField(15);
      field.addKeyListener( new KeyListener() {
      public void keyPressed(KeyEvent e) {
      setField(pressed,e);
      }
      public void keyReleased(KeyEvent e) {
      setField(released,e);
      }
      public void keyTyped(KeyEvent e) {
      setField(typed,e);
      if (field.getText().length() == LIMIT) {
      e.consume();
      }
      }
      });
      JPanel p = new JPanel();
      p.add(new JLabel("Type Here ->"));
      p.add(field);
      panel.add(p);

      p = new JPanel(new GridLayout(2,4));
      p.add(new JLabel("Key Pressed"));
      p.add(new JLabel("Key Typed"));
      p.add(new JLabel("Key Released"));
      p.add(Box.createHorizontalStrut(10));

      JButton b = new JButton("Clear");
      b.addActionListener( new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      pressed.setText(null);
      typed.setText(null);
      released.setText(null);
      }
      });
      p.add(pressed);
      p.add(typed);
      p.add(released);
      p.add(b);
      panel.add(p);

      getContentPane().add(panel);
      pack();
      addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
      System.exit(0);
      }
      });
      show();
      }
      private void setField(JTextField tf, KeyEvent e) {
      tf.setText("Code: " + e.getKeyCode() + " Char: " + e.getKeyChar
      ());
      }
      }
      (Review ID: 100778)
      ======================================================================

            tprinzing Tim Prinzing (Inactive)
            skonchad Sandeep Konchady
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: