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 );