0

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;
      }
    });
4
  • 1
    I would think that using a flag would solve the problem, but I assume that it does not. Can you edit your question and post a minimal reproducible example? Then I can reproduce your problem and debug it. I assume that your "data" comes from a database. If so, then in the code that you post you should hard-code the data to load.
    – Abra
    Commented Oct 25, 2023 at 5:19
  • I suspect you're blocking the Event Dispatch Thread when loading data. I second the call for a complete minimal reproducible example that we can compile, run, and execute trillions of tests to see where the problems lie. Commented Oct 25, 2023 at 8:17
  • Could this be a XY problem? Consider your main goal (are you just trying to lazy load a list) and maybe there is a better approach to that (e.g. working with a ListModel instead) Commented Oct 25, 2023 at 9:56
  • My goal is to disable or stop MouseWheelListener/MouseWheelEvent from listening. I changed my implementation to use MouseWheelListener instead of AdjustmentListener. I am facing the same issue with both the approach. I have a list of records, once I reach a specific record, I want the listener to stop listening anymore scroll/mouse drag events. MouseWheelListener mouseListener = new MouseWheelListener() { @Override public void mouseWheelMoved(MouseWheelEvent e) { //if a specific record is found, do not listen to further mouse scroll events } } Commented Oct 26, 2023 at 11:56

0