Java - Add JEditorPane into a JScrollPane and scroll to the very bottom initially

266 views Asked by At

I have a JEditorPane using "text/html" type and I have added the editorPane to a JScrollPane. Everything is good except it shows the very bottom of the JEditorPane when start. I want to display the very top by default.

Here is my code:

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

There are 1 answers

0
Allen On BEST ANSWER

By using

disclaimerContentPane.setCaretPosition(0);

can solve this issue