How Java Swing keeps the vertical scroll bar at the bottom
See picture to speak:
The middle Arrow refers to a Jtextpane
Jtextpane was wrapped in JScrollPane.
//Execution results display window Resulttextpane = new Assistpopuptextpane () Resulttextpane.setcontenttype ( "text/html; Charset=utf-8 ") ; Resulttextpane.seteditable (false) Defaultcaret caret = (Defaultcaret) resulttextpane.getcaret () Caret.setupdatepolicy (Defaultcaret _update) ; Scrollpane.setviewportview (resulttextpane)
When a push message is received, the content in the Jtextpane dynamically increases to a certain extent, resulting in a vertical scrollbar, so how do you keep the vertical scroll bar at the bottom of Jtextpane?
This way, you can always see the latest push messages.
Realize:
(1) Setting the Jtextpane policy
DefaultCaret caret = (DefaultCaret)resultTextPane.getCaret(); caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
(2) When Jtextpane dynamically adds content
resultTextPane.setSelectionStart(resultTextPane.getText().length()); JScrollBar vertical = scrollPane.getVerticalScrollBar(); vertical.setValue( vertical.getMaximum());
Description
(1) The type of Resulttextpane is Jtextpane;
(2) The difference between Jtextpane and JTextArea:
Jtextpane can set HTML style;
JTextArea cannot be set as HTML style
Projects are built with MAVEN
Source:
Project Source Code
Dependent jar Packages:
Dependency Packages
How Java Swing keeps the vertical scroll bar at the bottom