Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

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

843805Apr 20 2007 — edited Apr 20 2007
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
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) {
      }     
    });
  }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 18 2007
Added on Apr 20 2007
9 comments
1,039 views