I need to get certain line from text component with multiline support. So it either JTextArea or JTextPane.
How to get line 1 , 2 or .. etc? For example get line3 from text below
line1
line2
line3
line4
And is it possible to set another value for some line? For examp. set lineNew instead of line2
line1
lineNew
line3
line4
Is there any way?
To grab the text in a
JTextComponent, use thegetText()method which will return aString.Then to get the lines, split the string on
\n.Now if you want to modify the text, you can use the
setText(String)method.There are also a few other methods that you can use to change the text like
insert(String,int),append(String), andreplaceRange(String,int,int). All of this is documented in the javadocs.