title: Move JScrollPane scroll to the right SWING
body: <p>I am using JscrollPane that I add elements and when I have more than 7, the JScrollBar inside my JScrollPane is activated. I need that when I introduce a new element, the Scroll bar is moved to the right</p>
  
  <pre><code>    public void addElement(String nombre, Wheel wheel)
  {
      if(actual != null)
      {
          actual.bordeOn(false);
      }
      if(this.actual != null &amp;&amp; this.index != 0 &amp;&amp; this.index != this.actual.getIndex()+1)
      {//Se ha producido navegacion en medio de la pila
          for(int i = this.stack.size()-1 ; i &gt; this.actual.getIndex(); i--)
          {//Borramos todos los elementos del actual en adelante.
              BreadCrump b = this.stack.get(i);
              this.panelBCD.remove(b);
              this.stack.remove(i);
          }
          this.index = this.actual.getIndex()+1;
  
  
      }
      BreadCrump bc = new BreadCrump(nombre, this);
      bc.setIndex(this.index);
      this.index++;
      this.stack.add(bc);
      panelBCD.add(bc);    
      actual = bc;
      bc.setWheel(wheel);
      bc.bordeOn(true);
      numberElements++;
      JScrollBar scroll = sp.getHorizontalScrollBar();
      scroll.setValue(scroll.getMaximum());
      this.revalidate();
      this.repaint();
  
  }
  </code></pre>
  
  <p>CONSTRUCTOR:</p>
  
  <pre><code>public Displayer(JPanel father)
  panelBCD = new JPanel();
      this.setBackground(new java.awt.Color(200, 200, 200));
      this.setPreferredSize(new java.awt.Dimension(father.getSize().width, height));
      BoxLayout aaa = new BoxLayout(this,1);
      this.setLayout(aaa);
      FlowLayout mg = new FlowLayout(0,80,0); //Este es la separacion entre elementos
      //a.setLayout(null);
      panelBCD.setLayout(mg);
      mg.setAlignment(FlowLayout.LEFT);
      sp = new JScrollPane(panelBCD);
      sp.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
      sp.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
      //sp.setBounds(0, 0,father.getSize().width, height);
  
      this.add(sp);
      father.add(this, java.awt.BorderLayout.PAGE_END);
      addMouseListener(this);
  addMouseMotionListener(this);
      sp.setViewportBorder(new LineBorder(Color.BLACK));
  </code></pre>
  
  <p>The scroll bar is moved to the right but never reach the maximum, always have a little more distance to scroll.</p>
  
  <p>Anyone knows why occurs this? I saw that using setValue and getMaximum moves the scrollbar to the right, but for me it stays near the right but not right.</p>
  
  <p>Thanks for your time.</p>
  
  <p>Here is a screenshot to see it.</p>
  
  <p><a href="https://i.stack.imgur.com/5RW5v.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5RW5v.png" alt="enter image description here"></a></p>
  
  <p>EDIT: The class that contains that code is a JPanel</p>
  
  <p>EDIT2: The problem is that when I move right, the scroll is not updated, so the maximun when I setValue, is not the final maximun.</p>
  
  <p>This JPanel is inside another JPanel that have a layout with other components.</p>
  
  <p>So: in this JPanel i have a JScrollBarPane with a JPanel that have the components that I add in run time. This JPanel is inside another JPanel. When I update the scrollbar position, tha maximun position is not equal to the final maximun position of this iteration.</p>
  
  <p>The JScrollPane doenst affect to the size of the window</p>
  
  <p>I hope this is enough info.</p>