在 JTextPane 中逐行读取文本

发布于 2024-10-29 13:15:20 字数 1468 浏览 3 评论 0原文

我正在编写一个带有语法突出显示的编辑器。我想要做的是获取当前行并解析它而不是整个文档。问题是我可能做错了,但我不知道为什么。 这就是我正在做的事情:

public void keyReleased(KeyEvent e) {
    System.out.println(e);
    try {
    int inizio = Utilities.getRowStart(ta, ta.getCaretPosition());
    int fine = Utilities.getRowEnd(ta, ta.getCaretPosition());
    System.out.println("Inizio: " + inizio + " - Fine: " + fine);
    Element root = ta.getDocument().getDefaultRootElement();
    if(inizio == fine) System.out.println("Mi trovo ad inizio riga");
    else {
        int linea = RXTextUtilities.getLineAtCaret(ta);
        System.out.println("Linea: " + linea);
        Element child = root.getElement(linea-1);
        System.out.println("Testo corrente: " + child.getDocument().getLength() );
    }
    } catch (BadLocationException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

我对使用 javax.swing.text.Element 的理解是,我可以获得我想要的行(子项),然后对其进行处理,但 child 。 getDocument.getLength() 给我整个文档的长度。我做错了什么?

编辑#1:

child.getDocument.getLength() 给我文档开头的长度,我期望当前行的长度。请参阅:我使用 int linea = RXTextUtilities.getLineAtCaret(ta); 获取行号,而不是使用 root.getElement(linea-1) 提取行,所以我希望 child.getDocument ().getLength() 会给我当前行的长度。

RXTextUtilities 链接: http://www.camick.com/java/source/RXTextUtilities.java< /a>

I'm writing an editor with syntax highlighting. What I want to do is getting the current line and just parse it instead of the whole document. The problem is that I'm probably doing it wrong but I don't know why.
Here's what I'm doing:

public void keyReleased(KeyEvent e) {
    System.out.println(e);
    try {
    int inizio = Utilities.getRowStart(ta, ta.getCaretPosition());
    int fine = Utilities.getRowEnd(ta, ta.getCaretPosition());
    System.out.println("Inizio: " + inizio + " - Fine: " + fine);
    Element root = ta.getDocument().getDefaultRootElement();
    if(inizio == fine) System.out.println("Mi trovo ad inizio riga");
    else {
        int linea = RXTextUtilities.getLineAtCaret(ta);
        System.out.println("Linea: " + linea);
        Element child = root.getElement(linea-1);
        System.out.println("Testo corrente: " + child.getDocument().getLength() );
    }
    } catch (BadLocationException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

My understaing of using javax.swing.text.Element was that I can get just the line I want (the child) and than work on that but child.getDocument.getLength() gives me the length of the whole document. What am I doing wrong?

edit #1:

child.getDocument.getLength() gives me the length form the start of the document, I would expect the current line length. See: I got my line number using int linea = RXTextUtilities.getLineAtCaret(ta); than I extract the line using root.getElement(linea-1) so I would expect that child.getDocument().getLength() would give me the length of the current line.

RXTextUtilities link: http://www.camick.com/java/source/RXTextUtilities.java

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘和柠 2024-11-05 13:15:20

对于您的特定情况,您可以获得整个文档的长度,因为 child.getDocument() 实际上返回嵌入子级的文档。

您可以使用 child.getStartOffset()child.getEndOffset() 确定文档中的开始和结束,然后从整个文档中剪切出文本。

int start = child.getStartOffset();
int end = child.getEndOffset();
int length = end - start;
String text = child.getDocument().getText(start, length);

请注意,文本还包含 EOL 分隔符。

For your specific case you get the length of the whole document, because child.getDocument() actually returns the document in which the child is embedded.

You can use child.getStartOffset() and child.getEndOffset() to determine start and end within the document and then cut out the text from the whole document.

int start = child.getStartOffset();
int end = child.getEndOffset();
int length = end - start;
String text = child.getDocument().getText(start, length);

Please note that text then also contains a EOL delimiter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文