Question

I'm try to add a keyListener to a JTextField (I want to parse the text each time I press a key), but the text is modified AFTER the keyListener is called...

Is there any way to change the text before parsing it?

Here is the code :

exampleTextField.addKeyListener(new java.awt.event.KeyAdapter() {
    public void keyPressed(java.awt.event.KeyEvent evt) {
           String text = parsing(); //this method is called before te text is modified
           randomMethod(text);
       }
});

Thank you!

Was it helpful?

Solution

You should be using a DocumentListener instead.

textField.getDocument().addDocumentListener(...);

More info in the API: http://docs.oracle.com/javase/7/docs/api/javax/swing/text/Document.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top