I would like to insert text into JTextArea
at the current caret position how to do it? I have found only java script tutorials.
Using textarea.setText() ; will replace the whole content of your text. Instead of that, you have to use insert() method of your text object.
textarea.insert("My String Here", textarea.getCaretPosition());
You can get the caret position by textObject .getCaretPosition() and start adding your text from there.
I also found this useful: https://stackoverflow.com/a/5255666/2655623
to sum up:
textarea.replaceSelection("");
textarea.insert("My String Here", textarea.getCaretPosition());
Look at the method getCaretPosition()
.
Returns the position of the text insertion caret for the text component.
使用getDocument().insertString
而不是setText
方法。
textarea.getDocument().insertString(textarea.getCaretPosition(), t, null);
You can do the following. First set the Caret position and the insert your text.
textarea.setCaretPosition(int posintion)
textarea.setText(yourData);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.