2016-12-16 42 views
0

我有一個使用「text/html」類型的JEditorPane,並且我已將editorPane添加到JScrollPane。一切都很好,除了在啓動時顯示JEditorPane的底部。我想默認顯示最頂端。Java - 將JEditorPane添加到JScrollPane中,並滾動到最初的底部

這裏是我的代碼:

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

JScrollPane.getViewPort()scrollRectToVisible(新的Rectangle(0,0,1,1 )); – ControlAltDel

回答

0

通過使用

disclaimerContentPane.setCaretPosition(0); 

可以解決這個問題