得到改变的行jtextarea

时间:2012-02-09 00:41:55

标签: java swing jtextarea

我正在做一个需要自定义验证的文本编辑器。因为,内容非常大,我想只验证改变或添加的行。验证错误由行号显示,例如“Line:10不能超过15个字符”

  1. 对于单行,每次用户更改iam验证当前行时,保持行号作为参考。 - 已解决

  2. 用户可以复制文本和粘贴 - 多行。为此,想到了getSelectionStart和getSelectionEnd。有没有办法从getSelectionStart和getSelectionEnd获取行号,所以我可以得到起始行和结束行?

  3. 经过一些演示后,我认为选择可见的线条将解决我上面提到的第2号问题。

    Rectangle将解决获取可视区域的x,y坐标并编写代码,我想我差不多完成了。但是,我没有正确地得到最终行号,

          [code]
        //editor is jtextarea
          Rectangle r = editor.getVisibleRect();
          Point top = new Point(r.x, r.y);
          Point bottom = new Point(r.x, r.y + r.height);
          int startRow = editor.viewToModel(top); /* this is working. it shows 0 at           initial, then after the line reaches the end and when the scrollbar gets displayed, it shows the numbers correctly, 1,2,3...*/
         int endRow = editor.viewToModel(bottom); /* this is not working, when we type, it is taking column numbers */
         editorLineNo.setText(" START ROW " + startRow + " END ROW" + endRow);
         [/code]
    
         What is needed is, start row number and end row number from the viewable area  of jtextarea  
    

2 个答案:

答案 0 :(得分:4)

  

有没有办法获取行号...

Element root = textArea.getDocument().getDefaultRootElement();
int row = root.getElementIndex( selectionStart ) + 1;

答案 1 :(得分:3)

相关问题