Reputation: 59
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.
Upvotes: 2
Views: 1806
Reputation: 1067
You must change scrolls position after all components resizes and revalidated (e.g. after swing complete current update cycle). That's code works fine:
SwingUtilities.invokeLater(() -> {
scrollPane.getHorizontalScrollBar().setValue(scrollPane.getHorizontalScrollBar().getMaximum());
});
Upvotes: 3
Reputation: 6307
I cannot reproduce your issue even when adding new components to my scroll pane.
Using this code i get a perfectly right aligned scroll bar:
JScrollBar scroll = sp.getHorizontalScrollBar();
scroll.setValue(scroll.getMaximum());
Without more code it is hard to guess what is happening in your case. Is your jPanel the outer window? If resizing your scroll pane could effect the size of your window then you may need to call pack() on your window (jPanel/jFrame) before you use repaint().
If you are still stuck then break your code down into a Minimal, Complete, and Verifiable example and then run it for yourself, and you should be able to see what is causing the issue.
Edit:
As a test try your code in this order, and revalidate the scroll pane and the panel inside it rather than the parent jPanel:
numberElements++;
panelBCD.revalidate();
sp.revalidate();
JScrollBar scroll = sp.getHorizontalScrollBar();
scroll.setValue(scroll.getMaximum());
this.repaint();
Upvotes: 1