How Java Swing keeps the vertical scroll bar at the bottom

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.