-
Bug
-
Resolution: Duplicate
-
P2
-
None
-
1.3.0
-
generic, x86
-
generic, windows_98
Name: krT82822 Date: 01/30/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)
JTextField consumes the VK_ENTER key event which is stated
in the documentation. In a dialog, however, VK_ENTER
is supposed to activate the default button, even when
a JTextField has the focus (see bug id 4174465).
The API specification is aware of this problem and
suggests using the following workaround:
static {
JTextField f = new JTextField();
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
Keymap map = f.getKeymap();
map.removeKeyStrokeBinding(enter);
}
(from http://java.sun.com/products/jdk/1.3/docs/api/javax/swing/JTextField.html)
This piece of code used to work in 1.2.2 but does not work
anymore in 1.3.0RC1. This needs to be fixed...
30 Jan 2000 eval1127@eng -- confirmed (reproduced)
--
Example program:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTextFieldBug extends JDialog {
public JTextFieldBug() {
JPanel pnl = new JPanel();
pnl.setLayout(new GridBagLayout());
JTextField tf = new JTextField(24);
pnl.add(tf, new GridBagConstraints(
0, GridBagConstraints.RELATIVE, 1, 1, 0, 0,
GridBagConstraints.CENTER, GridBagConstraints.NONE,
new Insets(12, 12, 12, 12), 0, 0));
JButton but = new JButton("OK");
but.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
setVisible(false);
}
});
getRootPane().setDefaultButton(but);
pnl.add(but, 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);
}
public static void main(String[] args) {
new JTextField().getKeymap().removeKeyStrokeBinding(
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
new JTextFieldBug().setVisible(true);
}
}
(Review ID: 100532)
======================================================================
Name: krT82822 Date: 01/31/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)
In previous JDK versions, including 1.3 Beta, the "Enter" keystroke binding
could be removed from a JTextField by:
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
theTextField.getKeymap().removeKeyStrokeBinding(enter);
This feature is necessary, and widely used, to allow a default button to be
selected when pressing the Enter key inside of a JTextField (otherwise the
event is consumed by the JTextField and the default button is never selected).
Here is sample code. With JDK 1.2/1.3Beta, pressing Enter inside the JTextField
will cause the JButton to be selected. In 1.3 RC1, the JTextField is selected.
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
class KeyMapTest
{
public static void main(String[] args) {
JPanel contentPnl = new JPanel();
JTextField theTextField = new JTextField(20);
JButton theButton = new JButton("OK");
contentPnl.add(theTextField);
contentPnl.add(theButton);
KeyStrokeHandler theActionListener = new KeyStrokeHandler();
theTextField.addActionListener(theActionListener);
theButton.addActionListener(theActionListener);
/* Remove enter binding from JTextField. With JDK 1.2.2 and JDK 1.3
* Beta, pressing Enter from inside the JTextField will now activate
* the OK button. In 1.3 RC1, this no longer works. */
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
theTextField.getKeymap().removeKeyStrokeBinding(enter);
JFrame theFrame = new JFrame();
theFrame.getContentPane().add(contentPnl);
/* Make the OK button the default button, so that pressing Enter
* anywhere will cause the OK button to be selected. */
theFrame.getRootPane().setDefaultButton(theButton);
theFrame.setSize(new Dimension(350,350));
theFrame.show();
}
}
class KeyStrokeHandler implements ActionListener
{
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() instanceof JTextField) {
System.out.println("*** ENTER PRESSED IN TEXTFIELD!");
}
else if (evt.getSource() instanceof JButton) {
System.out.println("*** OK BUTTON PRESSED!");
}
}
}
(Review ID: 100569)
======================================================================
- duplicates
-
JDK-4306756 Can't remove VK_ENTER keybinding from JTextField so default button fires when JT
-
- Resolved
-