问题
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 too 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?
回答1:
I found a discussion of the same problem. Have a look at http://www.coderanch.com/t/329964/GUI/java/JScrollpane-Force-autoscroll-bottom
回答2:
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.
回答3:
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(...)
来源:https://stackoverflow.com/questions/2810344/how-to-scroll-the-contents-to-bottom-by-default-in-jtextpane