【问题标题】:Move JScrollPane scroll to the right SWING向右移动 JScrollPane 滚动 SWING
【发布时间】:2016-09-30 02:28:57
【问题描述】:

我正在使用添加元素的 JscrollPane,当我有超过 7 个元素时,我的 JScrollPane 中的 JScrollBar 被激活。我需要当我引入一个新元素时,滚动条向右移动

    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:问题是当我向右移动时,滚动没有更新,所以我设置值时的最大值,不是最终的最大值。

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

所以:在这个 JPanel 中,我有一个 JScrollBarPane 和一个 JPanel,其中包含我在运行时添加的组件。这个 JPanel 在另一个 JPanel 中。当我更新滚动条位置时,最大位置不等于本次迭代的最终最大位置。

JScrollPane 不会影响窗口的大小

我希望这是足够的信息。

【问题讨论】:

    标签: java swing jscrollpane jscrollbar


    【解决方案1】:

    您必须在所有组件调整大小并重新验证后更改滚动位置(例如,在摆动完成当前更新周期后)。那是代码工作正常:

    SwingUtilities.invokeLater(() -> {
      scrollPane.getHorizontalScrollBar().setValue(scrollPane.getHorizontalScrollBar().getMaximum());
    });
    

    【讨论】:

      【解决方案2】:

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

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

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

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

      如果您仍然卡住,请将您的代码分解为 Minimal, Complete, and Verifiable example,然后自己运行它,您应该能够看到导致问题的原因。

      编辑

      作为测试,按此顺序尝试您的代码,并重新验证滚动窗格和其中的面板,而不是父 jPanel:

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

      【讨论】:

      • 我看到了问题,但我不知道如何解决它。问题是当我做 scroll.setValue() 时,滚动条没有更新。如果在我的主代码中我强制它向右移动,它可以工作,但在我的面板代码中,不工作。我编辑消息以获取更多信息
      • @Transy 查看我的编辑。您需要调整代码的顺序并更改要重新验证的内容。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      • 1970-01-01
      • 2011-01-29
      • 1970-01-01
      相关资源
      最近更新 更多