As required by the project, a panel is customized, and a jtable table is filled in under jscrollpane. The cells in the table use the custom Renderer tabletextareacellrenderer (replace jlabel with jtextarea, the latter is actually the default defaultcellrenderer ). As required, the scroll bar will automatically scroll to the lowest end after the Panel is filled. It seems to be a simple problem. The implementation method is as follows:
Jscrollbar scrollbar = This. jscrollpane1.getverticalscrollbar ();
Scrollbar. setvalue (scrollbar. getmaxmium ());
The actual results are not ideal, especially when the number of rows in the cell content is large. So the second method of work ing is used to get the focus of the last row of cells, which naturally realizes the scroll bar to the bottom.
If (notetable. getrowcount ()! = 0 ){
Int rowcount = notetable. getrowcount ();
Notetable. getselectionmodel (). setselectioninterval (rowcount-1, rowcount-1); // You can select this cell.
Rectangle rect = notetable. getcellrect (rowcount-1, 0, false );
Notetable. scrollrecttovisible (rect );
}
The test result is similar to that of the first method, so I gave up and went on to the third method:
Jscrollbar scrollbar = This. jscrollpane1.getverticalscrollbar ();
Scrollbar. setvalue (scrollbar. GetModel (). getmaximum ()-scrollbar. GetModel (). getextent ());
There is still a problem when using it directly. After jscrollpane is built, the interface is automatically adjusted. After the content is filled, the above Code is called and scrollbar is found. getModel (). getmaximum () and scrollbar. getModel (). getextent () is still zero or unstable, which means that the automatic adjustment has not been completed, and the result is not that the value is set to the maximum value or the scroll bar is set to the bottom. The solution is to add a listener. When the listener is automatically adjusted, call the above method. However, another problem occurs at this time. What is the flag of automatic adjustment, and which event of the listener determines which attribute changes? My method is:
Jscrollpane1.getverticalscrollbar (). addadjustmentlistener (New adjustmentlistener (){
Public void adjustmentvaluechanged (adjustmentevent EVT ){
If (EVT. getadjustmenttype () = adjustmentevent. Track & isneedbottom <= 3) {response (). setvalue (jscrollpane1.getverticalscrollbar (). GetModel (). getmaximum ()
-Jscrollpane1.getverticalscrollbar (). GetModel (). getextent ());
Isneedbottom ++;
}
}
});
The red part means that automatic adjustment is in progress, and the blue part means three adjustments (because automatic adjustment is not performed once, you can check the local variables using debugging. In most cases, you can check the local variables twice, isneedbottom is a defined global integer variable. You can set it to 0 when the scroll bar automatically scrolls to the end. After testing, this method eventually meets the requirements.
Looking back, the listening status is a work ing method. You have not found the attributes that are automatically reflected in the adjustment. If you can monitor the changes to this attribute, the method is clearer and easier.