How do I use Caret to indicate which line it is on from the JTextPane? (Java)

Problem: I have CaretListener and DocumentListener listening on JTextPane.

I need an algorithm that can determine which line is the caret on the JTextPane, here's an example:

alt text

Result: 3rd line

alt text

Result: second line

alt text

Result: 4th line

and if the algorithm can determine which row is the carriage in JTextPane, fairly easy to tune all that is between parentheses, as picture (the carriage is a sign m

of metadata

):

alt text

-

This is how I divide all the text I have extracted from the JTextPane into sentences:

String[] lines = textPane.getText().split("\r?\n|\r", -1);

      

Sentences textPane

are separated by \ n.

The problem is, how can I manipulate the caret to tell me what position and line it is in? I know that the caret point says what position it is in, but I cannot figure out which line it is in . Assuming if I know which line the caret is on, I can just do lines[<line number>]

and manipulate the line from there.

In short: how to use a CaretListener and / or DocumentListener to find out where the caret line is currently on and get the line to process the lines further? Please help. Thanks.

Let me know if further clarification is required. Thank you for your time.

+2


a source to share


3 answers


Here is the source code you requested:



static int getLineOfOffset(JTextComponent comp, int offset) throws BadLocationException {
    Document doc = comp.getDocument();
    if (offset < 0) {
        throw new BadLocationException("Can't translate offset to line", -1);
    } else if (offset > doc.getLength()) {
        throw new BadLocationException("Can't translate offset to line", doc.getLength() + 1);
    } else {
        Element map = doc.getDefaultRootElement();
        return map.getElementIndex(offset);
    }
}

static int getLineStartOffset(JTextComponent comp, int line) throws BadLocationException {
    Element map = comp.getDocument().getDefaultRootElement();
    if (line < 0) {
        throw new BadLocationException("Negative line", -1);
    } else if (line >= map.getElementCount()) {
        throw new BadLocationException("No such line", comp.getDocument().getLength() + 1);
    } else {
        Element lineElem = map.getElement(line);
        return lineElem.getStartOffset();
    }
}

...

public void caretUpdate(CaretEvent e) {
    int dot = e.getDot();
    int line = getLineOfOffset(textComponent, dot);
    int positionInLine = dot - getLineStartOffset(textComponent, line);
    ...
}

      

+9


a source


Use the paragraph concept in StyledDocument with JTextPane.getStyledDocument.

With the cursor position, you know the current paragraph with StyledDocument.getParagraph (pos). Then you iterate over the paragraphs from StyledDocument.getRootElements and children to look for the current paragraph number, hence the current line count.



Sorry for my English.

+2


a source


This is how I divide all the text I got from the JTextPane into sentences:

Your solution is not very efficient.

First of all, only "\ n" is stored in the document no matter what OS is used. This way you can simplify the regex.

However, there is no need to use a regular expression to parse the entire document, since you only need 1 line in the document. Now that you know about the Element interface, you can make your code more efficient.

a) get the line as demonstrated earlier
b) now you can get the Element represented by that line from the root element c) you can now use the start and end offsets and the getText (...) method to get just the text for that particular strings.

+1


a source







All Articles