jTextpane的转到行实现 [英] Goto line implementation for jTextpane

查看:62
本文介绍了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....

推荐答案

您需要浏览内容以查找行尾.
除非您有一个庞大的文档,否则它应该不会太糟,例如:

You need to scan through the content to find the line endings.
Unless you have a huge document, this shouldn''t be too bad, something like:

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的转到行实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆