Question

Is there a way to read the contents of a JTextPane line by line? Much like the BufferedReader?

Was it helpful?

Solution

Element root = textPane.getDocument().getDefaultRootElement();

Once you get the root Element you can check to see how many child elements (ie. lines) exist. Then you can get each child Element and use the start/end offset methods to get the text for that particular line.

This would be more efficient than getting all the text in one big string and then splitting it again.

OTHER TIPS

The way that I've done this in the past is to use the getText method over my pane, then parse the string that is returned looking for the newline character '\n'.

Can you explain what you're trying to do? From the top of the head I can't say if it is possible actually to read it line by line. Of course you could just split the text by the newline character and then you would get an array of strings, each line as its own element. Is this a problem in your case?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top