title: How to scroll a ScrollPane in code?
body: <p>I’m trying to make a scrollwheel component like this in LibGDX:
  <a href="https://i.stack.imgur.com/pXkHt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pXkHt.png" alt="enter image description here"></a></p>
  
  <p>I’m using ScrollPane since it has input and fling handling built in. I have an image for the scrollwheel that is divided into 14 sections, the scrollpane itself is two sections shorter so that there will be one section on the right and left sides that it can scroll to in either direction. Once the scroll position reaches the end in either direction I want to reset the scroll position back to the center. Doing this over and over again should create the illusion of an infinite scrolling wheel (hopefully).</p>
  
  <p>The problem I’m having is how to position the ScrollPane in code to reset the image once it reaches either end. So far nothing I have tried to set the scroll position has worked. I’ve tried setScrollX() and scrollTo() methods. I’ve also tried setting the size of the scrollpane to be various sizes (same size as image and two sections smaller than the image). I’ve tried calling layout, invalidate, and pack on the scrollpane to make sure it is laid out correctly before I set the scroll value. I thought that updateVisualScroll() might force it to update the scroll position, but this also has no effect.</p>
  
  <p>No matter what I do it simply ignores all of my calls to change the scroll position so I’m clearly missing something. In my code below I'm trying to get the scrollwheel to start in the center of the image and instead it's starting position is all the way at the left.</p>
  
  <p>I also need to be able to get the current scroll position to detect when it has reached either end. I tried overriding the act() method and printing out scrollPane.getX(), but this value was always “0” even when I manually clicked and dragged it to scroll the ScrollPane.</p>
  
  <p>The scrolling does work when manually clicking and dragging, so I believe the ScrollPane is set up correctly, I just can’t get it to scroll within the code.</p>
  
  <p>Here is my code, and for simplicity I took all of my experimentation code out because none of my experimenting worked.</p>
  
  <pre><code>public class MyScrollWheel extends Container&lt;ScrollPane&gt; {
      private ScrollPane scrollPane;
      private Image image;
      private int scrollOffset;
  
      public MyScrollWheel(){
          Texture texture = new Texture(Gdx.files.internal("internal/scrollwheel.png"));
          image = new Image(texture);
  
          scrollOffset = (int)(image.getWidth()/14);
  
          scrollPane = new ScrollPane(image);
          scrollPane.setOverscroll(false, false);
  
          setActor(scrollPane);
          size(image.getWidth()-(scrollOffset*2), image.getHeight());
  
          scrollPane.setScrollX(scrollOffset); // &lt;&lt; this doesn't scroll
          scrollPane.updateVisualScroll();
      }
  }
  </code></pre>