I’ve got a JTable that updates frequently. It sits inside of a scroll pane.
Occasionally we’ll see a message that we’d like to dig deeper into. Unfortunately due to the number of updates the message will scroll off the visible screen before we can finish looking at it.
I’d like to be able to freeze the viewport/scrolling while we examine the message but still allow the table model to update with new messages.
I can get the selected row to go to the top with this code:
int firstSelectedRow = table.getSelectedRow();
Rectangle rowLocation = table.getCellRect(firstSelectedRow, 0, false);
scroll.getVerticalScrollBar().setValue(rowLocation.y);
freezeScrolling.setText("Resume Updates");
but that only happens on button press and then it quickly sscrolls away.
Is there a way to tell the viewport/scroll pane to freeze on a selection and the to turn it off so that the scroll pane updates as you’d expect?
OK, this one teased me a lot, so I spent some time to find a way for this. I found two options:
Here is an SSCCE that illustrates both mechanisms (don’t mind the crapiness of the code design, I tested a bunch of stuffs — extending the JScrollPane is actually not a good idea, it can all be done externally).