我正在使用JscrollPane添加元素,当我有7个以上时,我的JScrollPane中的JScrollBar被激活。我需要的是,当我介绍一个新的元素,滚动条移动到右侧将JScrollPane滚动向右移动SWING
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();
}
的构造函数:
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));
滚动条向右移动,但从未达到最大,总有多一点距离滚动。
任何人都知道为什么发生这种情况?我看到使用setValue和getMaximum将滚动条移动到右侧,但对我来说,它保持靠近右侧但不是正确的位置。
谢谢你的时间。
下面是截图。
编辑:包含代码的类是一个JPanel
EDIT2:问题是,当我向右移动,滚动没有更新,所以当我SETVALUE的maximun,是不是最后的最大值。
此JPanel位于另一个具有其他组件的布局的JPanel内。
因此:在这个JPanel中,我有一个带有JPanel的JScrollBarPane,它具有我在运行时添加的组件。这个JPanel在另一个JPanel中。当我更新滚动条位置时,最大位置不等于此迭代的最终最大位置。
JScrollPane的doenst影响到窗口
我希望这是足够的信息的大小。
我看到这个问题,但我不知道我该如何解决它。问题是,当我做scroll.setValue(),滚动条不更新。如果在我的主代码中强制它移动到右侧,它可以工作,但在我的Panel代码中,不工作。我编辑信息以获取更多信息 – Transy
@Transy查看我的编辑。您需要调整代码的顺序并更改要重新验证的内容。 – sorifiend