本文介绍了jTextpane的转到行实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在应用程序中使用了Jtextpane.我想切换到想要的行号.如何为jTextpane在Java中实现Goto行功能.
请帮忙....
I am using a Jtextpane in my application. I want to switch to line number I want . How can I implement Goto line feature in java for jTextpane.
please help....
推荐答案
private void setLineNumber(JTextPane text, int line) {
int currentLine = 0;
int currentSelection = 0;
String textContent = text.getText();
String seperator = System.getProperty("line.seperator");
int seperatorLength = seperator.length();
while (currentLine < line) {
int next = textContent.indexOf(seperator,currentSelection);
if (next > -1) {
currentSelection = next + seperatorLength;
currentLine++;
} else {
// set to the end of doc
currentSelection = textContent.length();
currentLine= line; // exits loop
}
}
text.setCaretPosition(currentSelection);
}
这篇关于jTextpane的转到行实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!