Jtextarea+jscrollpane scroll bar automatically at the bottom (reprint)

Source: Internet
Author: User

This is the process of my production Gobang problems encountered in the online search several kinds of answers, listed below. But the first kind of feeling is quite convenient. Use simple, cool!

1. Use JTextArea's SelectAll () method to force the cursor to be moved to the last line after adding information. It is said to be used in the Aviva.

2. Use JTextArea's setcaretposition (); Manually set the cursor position to the last row. The popularity is quite high. The method of use is also very simple, as follows: Textarea.setcaretposition (Textarea.getdocument (). GetLength ());

3. (copy) after JTextArea has loaded the automatic scrollbar Jscroll, add JTextArea to the viewport of Jscrolpanel: (There are some bugs that make the image a little blinking)
Recvscrollpane.getviewport (). Add (Recvarea, NULL);
Then, after JTextArea inserts the last new message, the viewport of the scrollbar is reset to the bottom of the line:
int height = 20;
Point P = new Point ();
P.setlocation (0, Recvarea.getlinecount () * height);
Recvscrollpane.getviewport (). Setviewposition (P);

4. Tricky treatment on the internet to say in English

Jtextpane pane = new Jtextpane () ...//Add some text to the Jtextpane ...
Document doc = Pane.getdocument ();
Pane.select (Doc.getlength (), doc.getlength ()); This should enforce the viewport move to where the line being selected.
...

5. This seems to be the most common method, but the actual usage is only to the penultimate line. Or stick it out?

Jscrollbar Sbar=spcontainer.getverticalscrollbar ();
Sbar.setvalue (Sbar.getmaximum ());

Jtextarea+jscrollpane scroll bar automatically at the bottom (reprint)

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.