Ben G
Ben G

Reputation: 253

How to release JScrollpane to make it be not always down to the bottom?

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());
        }
    });
}

Upvotes: 3

Views: 303

Answers (1)

Alex Stybaev
Alex Stybaev

Reputation: 4693

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;
  }
}

Upvotes: 3

Related Questions