Computer Science Canada

How to use Backspace to remove a character from a String.

Author:  mike200015 [ Fri Apr 20, 2007 6:49 pm ]
Post subject:  How to use Backspace to remove a character from a String.

I have a text area with a document listener (below). When a user types into it, the input is saved into the string userInput, and the text is then removed from the text area so it is not displayed (using invokeLater). I need the Backspace key to do what it is supposed to do, remove the last character from the userInput string, but I don't know how to do that.

Please help,
Thanks
code:

typedArea.getDocument().addDocumentListener(new DocumentListener() {
      Document doc = (Document)typedArea.getDocument();
      public void insertUpdate(DocumentEvent e) {
        try {
          userInput += typedArea.getText(doc.getLength()-1, 1);
          System.out.println(userInput);
          Runnable clearText = new Runnable() {
            public void run() {
              typedArea.setText("");
            }
          };
          SwingUtilities.invokeLater(clearText);
        }
        catch(Exception ex) {
        }
 
      }
      public void removeUpdate(DocumentEvent e) {
      }
      public void changedUpdate(DocumentEvent e) {
      }     
    });
  }


: