View the jdk api documentation. We know that jtable has two functions: setcolumnselectioninterval (INT beginindex, int endindex), which can be used to select the specified column; setrowselectioninterval (INT beginindex, int endindex), you can select the specified row. When the jtable contains many columns or rows, it is okay to call these two functions. However, this may occur when the specified row or column has been selected, the row or column is not in the visible area. You need to drag the scroll bar to view the selected row or column. In this way, the user experience is not very good.
The most direct idea is to see if jtable provides APIs for calling. For jtree, the following functions are available: scrollpathtovisible (treepath path) and scrollrowtovisible (INT row ). Call one of the functions to achieve the goal. Jtable does not provide such a function. Jcomponent provides the scrollrecttovisible (rectangle rect) function ). Through this function, you can still implement jtable scrolling. The scrollrecttovisible function forwards the call to its parent container for processing. The parent container chooses to continue forwarding or process the request.
The scrollrecttovisible function receives only one rectangle parameter, indicating which region you want to be visible. For jtable, obtain the required parameters in the following way:
Int x = jtable1.getbounds (). X;
Int y = jtable1.getrowheight () * rowindex;
Int width = jtable1.getbounds (). width;
Int Height = jtable1.getrowheight ();
Jtable1.scrollrecttovisible (New rectangle (X, Y, width, height ));
Jlist can be implemented as follows:
Jlist1.scrollrecttovisible (jlist1.getcellbounds (rowindex, rowindex ));