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
public void addElement(String nombre, Wheel wheel)
{
if(actual != null)
{
actual.bordeOn(false);
}
if(this.actual != null && this.index != 0 && this.index != this.actual.getIndex()+1)
{//Se ha producido navegacion en medio de la pila
for(int i = this.stack.size()-1 ; i > 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();
}
CONSTRUCTOR:
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));
The scroll bar is moved to the right but never reach the maximum, always have a little more distance to scroll.
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.
Thanks for your time.
Here is a screenshot to see it.
EDIT: The class that contains that code is a JPanel
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.
This JPanel is inside another JPanel that have a layout with other components.
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.
The JScrollPane doenst affect to the size of the window
I hope this is enough info.