Q:

Is it Possible to undo a typed character into a JText area?

Is it possible to remove the character typed into a JTextArea in Java Swing? Or block certain characters from being added? I am not talking about a replace, I specifically want to remove / block the character added by a key event. Here's some example code to illustrate what I want to do:
      input.addKeyListener(new KeyListener(){

        @Override
        public void keyTyped(KeyEvent e){
            //Nothing to do here
        }

        @Override
        public void keyPressed(KeyEvent e){
            if(condition == true){
            e.undo();
            //I want to remove the key that character that was inserted with this action
            }
        }

        @Override
        public void keyReleased(KeyEvent e){
            //Nothing to do here
        }

    });

Thanks in advance!

A:

You can store the content of the textfield at the end of the keyPressed method.
If you then encounter a key, that you don't want, you can set the content of the textfield to the stored value and return.
private String oldValue;

@Override
    public void keyPressed(KeyEvent e){
        if(condition == true){
            setText(oldValue);
            return;
        }
        oldvalue = getText();//Replace getText() with correct getter for current value
    }

