Reputation: 3150
I am doing a text editor which needs custom validation. Since, the content is very large i thought of validating only the lines which are changed or added. The validation errors are shown by the line numbers, like "Line: 10 cannot exceed 15 chars"
For single lines, every time the user changes iam validating the current line, keeping the row number as reference. - solved
The user can copy text and paste - multiple lines. for this, thought of getSelectionStart and getSelectionEnd. Is there a way of getting the row numbers from getSelectionStart and getSelectionEnd, so i can get starting row and ending row?
After some exprements i thought selecting the lines which are visible will solve my above statated No. 2 problem.
Rectangle would solve in getting the x, y cordinates of viewable region and wrote the code, i think i am almost finished. But, i am not getting the end row number correctly,
[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
Upvotes: 2
Views: 277
Reputation: 324207
Is there a way of getting the row numbers ...
Element root = textArea.getDocument().getDefaultRootElement();
int row = root.getElementIndex( selectionStart ) + 1;
Upvotes: 4