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

Can't remove VK_ENTER keybinding from JTextField so default button fires when JT

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P2 P2
    • 1.3.0
    • 1.3.0
    • client-libs
    • rc2
    • generic, x86
    • generic, windows_98, windows_nt



        Name: clC74495 Date: 01/24/2000


        In JDK1.2.2 you could use:
          static {
            // This is the JDK1.2.2 way to enable default buttons with textfields.
            JTextField f = new JTextField();
            KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
            Keymap map = f.getKeymap();
            map.removeKeyStrokeBinding(enter);
          }
        to remove the JTextField VK_ENTER keybinding. This has no effect anymore
        with the new InputMap and ActionMap.
        Unfortunately, the new way:
            // This should work in jdk1.3, but it doesn't
            jTextField1.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
        doesn't work either due to a bug in InputMap and ActionMap.

        The unregisterKeyboardAction code calls Input.get to find out if
        the keybinding exists, and that code travels the parent chain till it
        finds a keybinding. Then Input.remove is called, which ONLY removes
        the keybinding from the current InputMap. Obviously remove will not
        work in all cases where the keybinding is in one of the parents.
        Same for ActionMap.

            public void unregisterKeyboardAction(KeyStroke aKeyStroke) {
        ActionMap am = getActionMap(false);
        for (int counter = 0; counter < 3; counter++) {
        InputMap km = getInputMap(counter, false);
        if (km != null) {
        Object actionID = km.get(aKeyStroke); // See InputMap.get

        if (am != null && actionID != null) {
        am.remove(actionID); // See InputMap.remove
        }
        km.remove(aKeyStroke);
        }
        }
            }


            public Object get(KeyStroke keyStroke) { // travels the parents
        if (arrayTable == null) {
        InputMap parent = getParent();

        if (parent != null) {
        return parent.get(keyStroke);
        }
        return null;
        }
        Object value = arrayTable.get(keyStroke);

        if (value == null) {
        InputMap parent = getParent();

        if (parent != null) {
        return parent.get(keyStroke);
        }
        }
        return value;
            }

            /**
             * Removes the binding for <code>key</code> from this
             * <code>InputMap</code>.
             */
            public void remove(KeyStroke key) {
        if (arrayTable != null) {
        arrayTable.remove(key); // ONLY do the current one
        }
            }


        -------------------------------------------------
        //Source for Frame1.java

        package defaultButton;

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

        public class Frame1 extends JFrame {
          JPanel contentPane;
          BorderLayout borderLayout1 = new BorderLayout();
          JButton jButton1 = new JButton();
          JTextField jTextField1 = new JTextField();

          /**Construct the frame*/
          public Frame1() {
            enableEvents(AWTEvent.WINDOW_EVENT_MASK);
            try {
              jbInit();
            }
            catch(Exception e) {
              e.printStackTrace();
            }
          }

          static {
            // This is the JDK1.2.2 way to enable default buttons with textfields.
            JTextField f = new JTextField();
            KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
            Keymap map = f.getKeymap();
            map.removeKeyStrokeBinding(enter);
          }

          /**Component initialization*/
          private void jbInit() throws Exception {
            jButton1.setText("jButton1");
            jButton1.addActionListener(new java.awt.event.ActionListener() {

              public void actionPerformed(ActionEvent e) {
                jButton1_actionPerformed(e);
              }
            });
            //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
            contentPane = (JPanel) this.getContentPane();
            contentPane.setLayout(borderLayout1);
            this.setSize(new Dimension(400, 300));
            this.setTitle("Frame Title");
            jTextField1.setText("jTextField1");
            contentPane.add(jButton1, BorderLayout.NORTH);
            contentPane.add(jTextField1, BorderLayout.SOUTH);
            contentPane.getRootPane().setDefaultButton(jButton1);

            // This should work in jdk1.3, but it doesn't
            jTextField1.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));

          }

          /**Overridden so we can exit when window is closed*/
          protected void processWindowEvent(WindowEvent e) {
            super.processWindowEvent(e);
            if (e.getID() == WindowEvent.WINDOW_CLOSING) {
              System.exit(0);
            }
          }

          void jButton1_actionPerformed(ActionEvent e) {
            jButton1.setText("Fired default button");
          }
        }

        ----------------------------------------------
        // Source for Application1.java

        package defaultButton;

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

        public class Application1 {
          boolean packFrame = false;

          /**Construct the application*/
          public Application1() {
            Frame1 frame = new Frame1();
            //Validate frames that have preset sizes
            //Pack frames that have useful preferred size info, e.g. from their layout
            if (packFrame) {
              frame.pack();
            }
            else {
              frame.validate();
            }
            //Center the window
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Dimension frameSize = frame.getSize();
            if (frameSize.height > screenSize.height) {
              frameSize.height = screenSize.height;
            }
            if (frameSize.width > screenSize.width) {
              frameSize.width = screenSize.width;
            }
            frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
            frame.setVisible(true);
          }

          /**Main method*/
          public static void main(String[] args) {
            try {
              UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            }
            catch(Exception e) {
              e.printStackTrace();
            }
            new Application1();
          }
        }
        (Review ID: 100250)
        ======================================================================

              svioletsunw Scott Violet (Inactive)
              clucasius Carlos Lucasius (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: