Trying to get the start and end point of each line within a text pane

送分小仙女□ 提交于 2019-12-13 03:58:18

问题


Trying to get the start and end point of each line within a text pane: The text pane contains (note target is the end of each line not including the blank space line):

(blank space line) MVESMKKVAGMDVELTVEERN000TAQEGDHGSHVYTKQKEENKGGEDKLKMIREYRQMVETELKLICCDILDVLDKHDDDDDKVFYYKMKGDYHRYLAEFATGNDRKEAAENSLVAYKAASDIAMTELPPTHPIRLGLALNFSVFYYEILNSPDRACRLAKAAFDDAIAELDTLSEESYKDS00000VQVGQQRSDMQGDGKKKAAAEEQNKEALQDVEDENQtarget MVESMKKVAGMDVELTVEERN000TAQEGDHGSHVYTKQKEENKGGEDKLKMIREYRQMVETELKLICCDILDVLDKHDDDDDDVFYYKMKGDYHRYLAEFATGNDRKEAAENSLVAYKAASDIAMTELPPTHPIRLGLALNFSVFYYEILNSPDRACRLAKAAFDDAIAELDTLSEESYKDS00000VQVGQQRSDMQGDGKKKAAAEEQNKEALQDVEDENQtarget MVESMKKVAGMDVELTVEERN000TAQEGDHGSHVYTKQKEENKGGEDKLKMIREYRQMVETELKLICCDILDVLDKHDDDDDDDFYYKMKGDYHRYLAEFATGNDRKEAAENSLVAYKAASDIAMTELPPTHPIRLGLALNFSVFYYEILNSPDRACRLAKAAFDDAIAELDTLSEESYKDS00000VQVGQQRSDMQGDGKKKDDDDDDDEEQNKEALQDVEDENQtarget

//This is what I have

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

        Element one = root.getElement(0);
        while (one !=null){            
        int one1 = one.getStartOffset();
        int two1 = one.getEndOffset();
        System.out.println(one1);
        System.out.println(two1);
        one = root.getElement(two1);          
     }

This is what I get (1st and 2nd element) and then hangs: 0 1 1 232


回答1:


You mix model and view. Document is just model but amount of rows depends on view (width of content).

Use javax.swing.text.Utilities.getRowStart()/getRowEnd()

See an example of the code usage http://java-sl.com/tip_row_column.html



来源:https://stackoverflow.com/questions/14002238/trying-to-get-the-start-and-end-point-of-each-line-within-a-text-pane

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