Name: bk70084 Date: 01/06/98
The handling of KeyEvents in TextFields is inconsistent between win_95 and
Solaris. Under Solaris, the TextField handles the KeyEvent, and then any KeyListeners
are given the KeyEvent. Under Windows 95, however, the KeyListeners are given the
KeyEvent BEFORE the TextField handles it. The result of this is, when in the
KeyListener, it is unknown whether the text returned by TextField.getText() has been
updated with the new keypress.
1. Run the following program
2. Type a few characters in the top TextField
3. Press the "Check" Button
4. Notice that the characters reported in the bottom TextField and the characters
reported by the "Check" Button differ by one character under Windows 95, but
they are identical under Solaris.
//--------------MyFrame.java--------------------
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends Frame implements KeyListener {
Button checkButton;
TextArea inputarea, outputarea;
public MyFrame() {
setLayout(new BorderLayout());
inputarea = new TextArea();
inputarea.addKeyListener(this);
add("North", inputarea);
outputarea = new TextArea();
outputarea.setEditable(false);
add("South", outputarea);
checkButton = new Button("Check");
checkButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String current_text = inputarea.getText();
int current_length = current_text.length();
System.out.println("The current text is \"" + current_text +
"\" and it is " + current_length + " characters long.");
}
});
add("Center", checkButton);
setSize(400, 400);
show();
}
public void keyPressed(KeyEvent ke) {}
public void keyReleased(KeyEvent ke) {}
public void keyTyped(KeyEvent ke) {
if (ke.getSource() == inputarea) {
String current_text = inputarea.getText();
int current_length = current_text.length();
outputarea.append("The current text is \"" + current_text +
"\" and it is " + current_length + " characters long." + "\n");
}
}
public static void main(String args[])
{
MyFrame mf = new MyFrame();
}
}
(Review ID: 22728)
======================================================================