tags:

views:

83

answers:

3

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

A: 

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'.

SOA Nerd
A: 

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?

Carlos
+1  A: 
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.

camickr
Nice idea, but I cant quite figure out how to get to the contents of the element using the offset methods?
ApoY2k
Text components and a Document both has getText() methods that allow you to get all of the text or part of the text.
camickr