Read JTextPane line by line

自古美人都是妖i 提交于 2019-12-23 23:26:37

问题


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


回答1:


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.




回答2:


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




回答3:


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?



来源:https://stackoverflow.com/questions/3550656/read-jtextpane-line-by-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!