I have JTable with two scrollbars (horizontal and vertical). When I use scrollRectToVisible, it return me Rectangle and this action cause that vertical and horizontal scrollbars automatically move to specified rows/columns. How can I move automatically vertical scrollbar to desired place and horizontal scrollbar should stay inactive? This method should show me selected row but my JTable is very wide and I would like to avoid automatically movement (horizontal) at the beginning (left side) of track -> simple say, I expect that horizontal position is unchanged.
public void goToSelected() {
int selectedRow = this.getSelectedRow();
if (selectedRow >= 0)
this.scrollRectToVisible(this.getCellRect(selectedRow, 0, true));
}
The horizontal position might change because you are specifying a column index (0) and the specified column might be invisible. You can simply combine the values of the currently visible area and the cell’s vertical range to get the desired effect:
Rectangle target = getCellRect(selectedRow, 0, true), vis = getVisibleRect();
target.x = vis.x;
target.width = vis.width;
scrollRectToVisible(target);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With