Java - 将JEditorPane添加到JScrollPane并最初滚动到最底部

时间:2016-12-16 19:33:00

标签: java jscrollpane jeditorpane

我有一个JEditorPane使用" text / html"类型,我已将editorPane添加到JScrollPane。一切都很好,除了它在开始时显示JEditorPane的最底部。我想默认显示最顶部。

这是我的代码:

public class DisclaimerPage extends JPanel {
    private static final String DISCLAIMER_CONTENT = 
        "<h2>This is a H2 header</h2>" +                //Bold first line (Title)
        "<p> This is the first paragraph having many lines of text. Text Text Text"
        + "Text Text TextText Text TextText Text TextText Text TextText Text TextText Text Text</p>"    //Content
        + "<p> <b>Bold Second Paragraph</b>: there will be N number of paragraph after this.</p>"
        + "<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>"
        + "<p> Last Paragraph </p>";
    private JScrollPane disclaimerScrollPane;
    private JEditorPane disclaimerContentPane;

    public DisclaimerPage() {
        setLayout(new BorderLayout(0, 15));

        disclaimerContentPane = new JEditorPane(); 
        disclaimerContentPane.setEditable(false);
        disclaimerContentPane.setContentType("text/html");
        disclaimerContentPane.setText(DISCLAIMER_CONTENT);

        disclaimerScrollPane = new JScrollPane(disclaimerContentPane);
        disclaimerScrollPane.setPreferredSize(new Dimension(480, 360));

        this.add(disclaimerScrollPane, BorderLayout.CENTER);
    }
}

1 个答案:

答案 0 :(得分:0)

使用

disclaimerContentPane.setCaretPosition(0);

可以解决这个问题