Jscrollpane automatically scroll to the bottom

Source: Internet
Author: User

1. Add a line to jtable (in jscrollpane), then let it get the focus and the scroll bar will automatically come down!
Int rowcount = table. getrowcount ();
Table. getselectionmodel (). setselectioninterval (rowCount-1, rowCount-1 );
Rectangle rect = table. getcellrect (rowCount-1, 0, true );

// Table. repaint (); if needed

// Table. updateui (); if needed

Table. scrollrecttovisible (rect );

Note: Table. scrollrecttovisible (rect) must be after table. repaint () and Table. updateui () (if any). Otherwise, sometimes the scroll bar cannot scroll to the bottom and you will experience it yourself.

2. Directly manipulate the jscrollbar in jscrollpane
Jscrollpane spane = new jscrollpane (table );
Jscrollbar sbar = spane. getverticalscrollbar (); // get the jscrollbar
Specific operations:
Sbar. setvalue (INT value); // you can specify a specific position.
Int value = sbar. getvalue (); // obtain the current position of the jscrollbar.
Sbar. getmaximum (); // obtain the maximum value allowed.
Sbar. getminimum (); // obtain the allowed minimum value.

3. The jtextarea + jscrollpane scroll bar is automatically located at the bottom.
(1) After jtextarea inserts the last message, use selectall () to force the cursor to move to the end of jtextarea to implement automatic scroll of the scroll bar. (In Aviva)

(2) After jtextarea inserts the last message, use (jtextarea) recvarea. setcaretposition (recvarea. gettext (). length (), move the cursor to the end to automatically scroll the scroll bar.

(3) After jtextarea loads the automatic scroll bar jscroll, add jtextarea to the viewport of jscrolpanel: (there are some bugs that make the image a little flickering)
Recvscrollpane. getviewport (). Add (recvarea, null );
After jtextarea inserts the last new message, it resets the viewport of the scroll bar to the bottom position:
Int Height = 20;
Point P = new point ();
P. setlocation (0, recvarea. getlinecount () * Height );
Recvscrollpane. getviewport (). setviewposition (P );

4. Set jtextarea in jscrollpane to automatically bottom
Int Height = 10;
Point P = new point ();
P. setlocation (0, this. jtextarea1.getlinecount () * Height );
This. jscrollpane1.getviewport (). setviewposition (P );

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.