在 JTextPane 中逐行读取文本
我正在编写一个带有语法突出显示的编辑器。我想要做的是获取当前行并解析它而不是整个文档。问题是我可能做错了,但我不知道为什么。 这就是我正在做的事情:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于您的特定情况,您可以获得整个文档的长度,因为
child.getDocument()
实际上返回嵌入子级的文档。您可以使用
child.getStartOffset()
和child.getEndOffset()
确定文档中的开始和结束,然后从整个文档中剪切出文本。请注意,文本还包含 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()
andchild.getEndOffset()
to determine start and end within the document and then cut out the text from the whole document.Please note that text then also contains a EOL delimiter.