i would like to know how to use a DocumentListener/DocumentEvent in java to prevent the user from deleting a certain portion of text in a JTextField, like on the windows command prompt or unix Terminal..
they show the current working directory and you can’t delete past the > or $
can anyone help me? thanks
The problem with using adding on a DocumentListener is that you can’t tack back the part that was deleted or edited from within the listener, otherwise you’ll get an exception saying that you are trying to modify the contents when you have the notification. The easiest way I know is to subclass a Document, override remove on the Document and set the text field to use the document, like in my example below: