2016-05-31 125 views
1

我正在使用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将滚动条移动到右侧,但对我来说,它保持靠近右侧但不是正确的位置。

谢谢你的时间。

下面是截图。

enter image description here

编辑:包含代码的类是一个JPanel

EDIT2:问题是,当我向右移动,滚动没有更新,所以当我SETVALUE的maximun,是不是最后的最大值。

此JPanel位于另一个具有其他组件的布局的JPanel内。

因此:在这个JPanel中,我有一个带有JPanel的JScrollBarPane,它具有我在运行时添加的组件。这个JPanel在另一个JPanel中。当我更新滚动条位置时,最大位置不等于此迭代的最终最大位置。

JScrollPane的doenst影响到窗口

我希望这是足够的信息的大小。

回答

2

即使将新组件添加到我的滚动窗格中,我也无法重现您的问题。

使用此代码我得到一个完美的右对齐的滚动条:

JScrollBar scroll = sp.getHorizontalScrollBar(); 
scroll.setValue(scroll.getMaximum()); 

没有更多的代码是很难猜测什么是你的情况发生。你的jPanel是外窗户吗?如果调整滚动窗格的大小会影响窗口的大小,那么在使用repaint()之前,可能需要在窗口上调用pack()(jPanel/jFrame)。

如果仍然卡住,那么将代码分解为Minimal, Complete, and Verifiable example,然后自行运行,并且应该能够看到是什么导致了问题。

编辑

作为测试尝试按以下顺序代码,并重新验证滚动面板和它里面的面板,而不是父母的JPanel:

numberElements++; 
panelBCD.revalidate(); 
sp.revalidate(); 
JScrollBar scroll = sp.getHorizontalScrollBar(); 
scroll.setValue(scroll.getMaximum()); 
this.repaint(); 
+0

我看到这个问题,但我不知道我该如何解决它。问题是,当我做scroll.setValue(),滚动条不更新。如果在我的主代码中强制它移动到右侧,它可以工作,但在我的Panel代码中,不工作。我编辑信息以获取更多信息 – Transy

+0

@Transy查看我的编辑。您需要调整代码的顺序并更改要重新验证的内容。 – sorifiend