【发布时间】:2012-02-09 00:41:54
【问题描述】:
我正在做一个需要自定义验证的文本编辑器。由于内容非常大,我想只验证更改或添加的行。验证错误由行号显示,例如“行:10 不能超过 15 个字符”
对于单行,每次用户更改时,我都会验证当前行,并保留行号作为参考。 - 解决了
用户可以复制文本并粘贴 - 多行。为此,想到了 getSelectionStart 和 getSelectionEnd。有没有办法从 getSelectionStart 和 getSelectionEnd 获取行号,所以我可以得到起始行和结束行?
经过一些实验,我认为选择可见的线条将解决我上面提到的第二个问题。
矩形将解决获取可视区域的 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
【问题讨论】:
-
感谢您的回答,这非常有用,现在我想,只选择可见的代码。为此,我使用 Rectangle 来获取可见部分。剩下的唯一部分是将矩形 y 坐标转换为行,这里是代码,pastebin.com/AvCWqNzj
-
谢谢,camickr。我快到了,只有最后的行号没有正确显示,pastebin.com/UgKCHgcx – shiva0101 m