3

I added the following code segment to my project to force the JScrollPane automatically be down to the bottom after the user acting selection performance, however, when I tried to drag the scroll to go to the top, it still is forced to make the scroll down to the bottom, and I want to ask any solution to resolve it? Thanks in advance.

private void autoScrollToBottom() {
    sdPanel.getTabScrollPane().getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {

        @Override
        public void adjustmentValueChanged(AdjustmentEvent e) {
            e.getAdjustable().setValue(e.getAdjustable().getMaximum());
        }
    });
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Ben G
  • 253
  • 1
  • 5
  • 15

1 Answers1

3

try to use some boolean flag which will indicate if you should move your scrollbar. Set it to to true when you pereform a correct action then in your code:

@Override
  public void adjustmentValueChanged(AdjustmentEvent e) {
  if(isScrollingDownRequired) {
    e.getAdjustable().setValue(e.getAdjustable().getMaximum());
    isScrollingDownRequired = false;
  }
}
Alex Stybaev
  • 4,623
  • 3
  • 30
  • 44
  • +1 for `AdjustmentListener`; there's a related example [here](http://stackoverflow.com/a/7519403/230513). – trashgod May 23 '12 at 10:05