How to scroll the contents to bottom by default in JTextPane?
I have JTextPane on my window and i have JTextField. When i press enter enter in JTextField, the text gets added in JTextPane. Everything works fine and scrollbar to开发者_Go百科o appears on its own. But, it doesn't appear properly. It automatically scrolls to the beginning of the content in JTextPane. How do i keep the scrollbar to the end of JTextPane?
The provided link in the accepted answer is an old link. You may want to check out Text Area Scrolling for additional information. The entry is for a text area but I believe the information applies to a text pane as well.
You can try by invoking
scrollPane.scrollRectToVisible(new Rectangle(0,main.getBounds(null).height,1,1));
This should be the most working approach. Otherwise try to search out scrollPane.setValue(..)
or work with the caret: editorPane.setCaretPosition(...)
I found a discussion of the same problem. Have a look at http://www.coderanch.com/t/329964/GUI/java/JScrollpane-Force-autoscroll-bottom
精彩评论