How to implement temporarily disabling AdjustmentListener of JScrollPane in Java Swing

51 views Asked by At

I have a code to load data when scroll is performed. Suppose I scroll, the loadData method is called and is in progress. If during this time I keep scrolling, even though the scrollbar is not moving the loadData method gets called multiple times. I assume all the scroll events are stored somewhere and then executed one after the other. I want the scroll event to stop once the call is made to loadData.

verticalScrollBar.addAdjustmentListener(e -> {
  if (!e.getValueIsAdjusting()) {
    loadData();
  }
});

My goal is to reload the list for which this JScrollPane is used. I am implementing virtual scrolling. Initially I load few records and then on scroll I load records in batches. With the issue I am facing it calls the method loadData multiple times hence it loads more records than what I require adding a delay. I want all the scroll events to be nullified or removed as soon as the loadData method starts execution.

  • I tried using a wait dialogue to avoid further scrolling once call is made to loadData method.
  • I tried removing the listener using removeAdjustmentListener(), with this implementation the call does not even enter addAdjustmentListener.
  • I tried setting a flag 'loadInProgress '.
    
    boolean loadInProgress = false;
    verticalScrollBar.addAdjustmentListener(e -> {
        if(loadInProgress)
            return;
      if (!e.getValueIsAdjusting()) {
        loadInProgress = true;
        loadData();
        loadInProgress = false;
      }
    });
0

There are 0 answers