java - JTextArea, JTextField, how to replace text from the caret to the left with another string -


i'm making text processing program, , want if user press 'a' ('a' demonstration, have list of special characters that), replace characters caret left string.

i have tried "settext" method replaced moved caret , text component scrolled end, want caret not move anywhere else , text component not scroll end, how can code that(i overriding processkeyevent)?

any appreciated.

try use document filter:

    jtextarea textarea = new jtextarea();      ((abstractdocument) textarea.getdocument()).setdocumentfilter(new documentfilter() {                     @override         public void replace(filterbypass fb, int offset, int length, string text, attributeset attrs)                 throws badlocationexception {              if ("a".equals(text)) {                 text = "b";             }                             super.replace(fb, offset, length, text, attrs);         }     }); 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -