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

typedArea.getDocument().addDocumentListener(new DocumentListener(){

Document doc = (Document)typedArea.getDocument();

publicvoid insertUpdate(DocumentEvent e){

try{

userInput += typedArea.getText(doc.getLength()-1, 1);

System.out.println(userInput);

Runnable clearText =new Runnable(){

publicvoid run(){

typedArea.setText("");

}

};

SwingUtilities.invokeLater(clearText);

}

catch(Exception ex){

}

}

publicvoid removeUpdate(DocumentEvent e){

}

publicvoid changedUpdate(DocumentEvent e){

}

});

}

[1910 byte] By [mike200015a] at [2007-11-27 1:49:11]
# 1

That's what you get for trying to be too clever... I guess you'd have to install a KeyListener on the text area and listen on the DELETE / BACKSPACE as well. And what about navigating - say user wants to go back a few characters and insert something? At this point you should stop and reconsider your current approach of using a text area for input, while wanting to remove the actual contents.

kirillga at 2007-7-12 1:14:10 > top of Java-index,Desktop,Core GUI APIs...
# 2

:) You make a good point about navigating. Have any ideas as to how to incorporate that. The main thing is that the user must not see what they are typing.

Also, if I use a keyListener as well, how would I remove the last character from a String?

And btw, what is the key code for Backspace? I didnt see it under keyevent stuff.

mike200015a at 2007-7-12 1:14:10 > top of Java-index,Desktop,Core GUI APIs...
# 3

If you continue with the current approach, you will have to reimplement everything from the text pane, including copy-paste, smart navigation (shift-left, shift-right), backspace / delete, bidi support and what not. I would highly recommend an alternative approach, such as setting the foreground to be the same as the background (and the caret color as well). If you also set the selection background / foreground to the same color, you're pretty much done with the "illusion" of empty text pane. However, this still leaves a big hole in the general approach - what if the user wants to go back a few characters and correct something there? If you don't display the caret, the user is lost / confined to the last typed character only. And what about selecting a word and replacing it with something else?

You could end up with very complicated code that will be very difficult to maintain on one hand, and have frustrated users that don't have the full editing functionality that they're used to on the other hand. I personally feel that i shouldn't encourage you to continue on your current path by providing answers to what the text pane already provides (but that's only me). And remember - somebody will have to maintain your code...

kirillga at 2007-7-12 1:14:10 > top of Java-index,Desktop,Core GUI APIs...
# 4

I don't know if you know what this program is for however. This program is a typing test for grade 9/10 students at my school. So everything about navigation and seeing the caret in order to know their place is not an issue since the student should not be allowed to do any of these things.

The only thing they should be allowed to do is use backspace to delete the last character.

mike200015a at 2007-7-12 1:14:11 > top of Java-index,Desktop,Core GUI APIs...
# 5

And? Why can't you just use the text pane? What are you doing against copy-pasting in the text pane? If they can delete the last character, then why do you want to prevent them from going back a word and correcting it? Your limitations sound a little artificial, especially given an option to use the backspace key.

kirillga at 2007-7-12 1:14:11 > top of Java-index,Desktop,Core GUI APIs...
# 6

Think about any typing test program you have used. You can never navigate back through what you have written. Usually they don't even allow backspace, just whatever you type the first time is taken and you move on to the next character. We want the student however to be allowed to delete their last character if they are able to recognize they made a mistake, but not allow them to scroll through since they will loose their place, not to mention they cannot see what they have typed on the screen so navigating is pointless.

When the students are taking a real test to be marked, the program will be full screen and not resizable so they cannot switch out of it to another program to copy from. The text area as well will only read one character at a time, so even if they do copy and paste, it will only read the last character.

Remember that these tests are timed, so any time wasted by the student in trying to backspace to an error they remembered about will cost them alot of time, and thus they will not be able to finish the passage.

mike200015a at 2007-7-12 1:14:11 > top of Java-index,Desktop,Core GUI APIs...
# 7

> We want the

> student however to be allowed to delete their last

> character

So what happens when they press Backspace twice? Will you add some logic for that?

> When the students are taking a real test to be

> marked, the program will be full screen and not

> resizable so they cannot switch out of it to another

> program to copy from.

How does this make it "unswitchable"? Alt+Tab works on the OS level, not on the single window level.

> The text area as well will only

> read one character at a time, so even if they do copy

> and paste, it will only read the last character.

So what's the point of allowing copy functionality in the first place?

kirillga at 2007-7-12 1:14:11 > top of Java-index,Desktop,Core GUI APIs...
# 8

If they press backspace twice, then it will do what you would think, delete the last two characters.

Now you are going to say, well why not just let them navigate then if they can just backspace to where they want. The answer is as I stated previously; the text is hidden, and therefore they cannot navigate anywhere since they cannot see where they are navigating. But pressing backspace, they know which character they are on. If they can remember how many letters ago an error is, and they want to waste their time backspacing there, then they can, but they will loose time on trying to complete the passage which may cost them more marks in the end.

In OS X, I am not sure why it works, but in full screen mode nothing will work, not even Alt+Tab. The only way to switch out of the program would be to quit the program.

We may decide to completely remove the copy-paste functionality, but for now it's ok since it does no good anyhow.

mike200015a at 2007-7-12 1:14:11 > top of Java-index,Desktop,Core GUI APIs...
# 9

I would just use kirillg's suggestion. Make the foreground and background the same color.

Using the backspace character on an empy text component will cause a beep, so that would confuse the student as they would think they did something wrong.

Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html]How to Use Key Bindings[url]. You can always prevent the students from using the arrow keys or copy and paste by removing the key bindings from the text component.

Otherwise just create some custom component that uses a KeyListener and handles the keyTyped() event. If the character typed satisfies Character.isDefined(...) then you add the character to your a StringBuffer. Then you handle the keyPressed() event for the backspace key and remove the last character from the buffer.

camickra at 2007-7-12 1:14:11 > top of Java-index,Desktop,Core GUI APIs...