-
Bug
-
Resolution: Fixed
-
P4
-
1.1.4
-
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)
======================================================================