How to scroll the contents to bottom by default in JTextPane?

时间秒杀一切 提交于 2020-02-24 08:44:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!