Java 滚动 JScrollPane,其中 JPanel 位于底部

发布于 2024-11-09 18:13:17 字数 178 浏览 0 评论 0原文

我有一个 JScrollPane,里面有一个非常高的 JPanel,它是动态更改的,项目附加在其末尾。我想要的是滚动到上述 JScrollPane 的底部,以便新附加的项目在添加时立即可见(它们不是直接附加到滚动窗格,而是附加到其 JPanel,并且是私有对象,因此不能 我怎样才能简单地

将滚动窗格滚动到最底部? 提前致谢!

I have a JScrollPane with a very high JPanel inside, that is changed dynamically, items being appended at its end. What I want, is to scroll to the bottom of aforementioned JScrollPane in order for the newly appended items to be visible instantly on addition (they are not appended to the scroll pane directly, but to its JPanel, and are private objects, so cannot be referenced.

How can I simply have that scroll pane scroll to the very bottom?
Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

芯好空 2024-11-16 18:13:17

JComponent.scrollRectToVisible(矩形)。在 JPanel 实例上调用它。

EG

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class ScrollToNewLabel {

    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JPanel gui = new JPanel(new BorderLayout(3,3));
                final JPanel panel = new JPanel(new GridLayout(0,1));
                JScrollPane scroll = new JScrollPane(panel);
                scroll.setPreferredSize(new Dimension(80,100));
                gui.add(scroll, BorderLayout.CENTER);
                JButton addLabel = new JButton("Add Label");
                gui.add(addLabel, BorderLayout.NORTH);
                ActionListener listener = new ActionListener() {
                    int counter = 0;
                    public void actionPerformed(ActionEvent ae) {
                        panel.add(new JLabel("Label " + ++counter));
                        panel.revalidate();
                        int height = (int)panel.getPreferredSize().getHeight();
                        Rectangle rect = new Rectangle(0,height,10,10);
                        panel.scrollRectToVisible(rect);
                    }
                };
                addLabel.addActionListener(listener);
                JOptionPane.showMessageDialog(null, gui);
            }
        });
    }
}

屏幕截图

在此处输入图像描述

EG 2

此示例基于 Vincent 的 答案,使用 JScrollPane.getVerticalScrollBar()setValue(height)。其中 height 是面板的首选高度(以像素为单位)。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class ScrollToNewLabel {

    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JPanel gui = new JPanel(new BorderLayout(3,3));
                final JPanel panel = new JPanel(new GridLayout(0,1));
                final JScrollPane scroll = new JScrollPane(panel);
                scroll.setPreferredSize(new Dimension(80,100));
                gui.add(scroll, BorderLayout.CENTER);
                JButton addLabel = new JButton("Add Label");
                gui.add(addLabel, BorderLayout.NORTH);
                ActionListener listener = new ActionListener() {
                    int counter = 0;
                    public void actionPerformed(ActionEvent ae) {
                        panel.add(new JLabel("Label " + ++counter));
                        panel.revalidate();
                        int height = (int)panel.getPreferredSize().getHeight();
                        scroll.getVerticalScrollBar().setValue(height);
                    }
                };
                addLabel.addActionListener(listener);
                JOptionPane.showMessageDialog(null, gui);
            }
        });
    }
}

JComponent.scrollRectToVisible(Rectangle). Call that on the JPanel instance.

E.G.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class ScrollToNewLabel {

    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JPanel gui = new JPanel(new BorderLayout(3,3));
                final JPanel panel = new JPanel(new GridLayout(0,1));
                JScrollPane scroll = new JScrollPane(panel);
                scroll.setPreferredSize(new Dimension(80,100));
                gui.add(scroll, BorderLayout.CENTER);
                JButton addLabel = new JButton("Add Label");
                gui.add(addLabel, BorderLayout.NORTH);
                ActionListener listener = new ActionListener() {
                    int counter = 0;
                    public void actionPerformed(ActionEvent ae) {
                        panel.add(new JLabel("Label " + ++counter));
                        panel.revalidate();
                        int height = (int)panel.getPreferredSize().getHeight();
                        Rectangle rect = new Rectangle(0,height,10,10);
                        panel.scrollRectToVisible(rect);
                    }
                };
                addLabel.addActionListener(listener);
                JOptionPane.showMessageDialog(null, gui);
            }
        });
    }
}

Screen shot

enter image description here

E.G. 2

This e.g. is based on Vincent's answer, to use JScrollPane.getVerticalScrollBar().setValue(height). Where height is the preferred height of the panel in pixels.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class ScrollToNewLabel {

    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JPanel gui = new JPanel(new BorderLayout(3,3));
                final JPanel panel = new JPanel(new GridLayout(0,1));
                final JScrollPane scroll = new JScrollPane(panel);
                scroll.setPreferredSize(new Dimension(80,100));
                gui.add(scroll, BorderLayout.CENTER);
                JButton addLabel = new JButton("Add Label");
                gui.add(addLabel, BorderLayout.NORTH);
                ActionListener listener = new ActionListener() {
                    int counter = 0;
                    public void actionPerformed(ActionEvent ae) {
                        panel.add(new JLabel("Label " + ++counter));
                        panel.revalidate();
                        int height = (int)panel.getPreferredSize().getHeight();
                        scroll.getVerticalScrollBar().setValue(height);
                    }
                };
                addLabel.addActionListener(listener);
                JOptionPane.showMessageDialog(null, gui);
            }
        });
    }
}
七度光 2024-11-16 18:13:17

scrollRectToVisible(...) 和scrollBar.setValue(...) 是通用的解决方案。

您可能对滚动表单感兴趣,它可以确保当当您使用 Tab 键切换到某个组件时,表单将自动滚动以确保该组件在滚动窗格中可见。它在幕后使用scrollRectToVisible()。

scrollRectToVisible(...) and scrollBar.setValue(...) are the general solutions.

You may be interested in Scrolling a Form which ensures that when you tab to a component the form will scroll automatically to make sure the the component will be visible in the scrollpane. Behind the scenes it uses scrollRectToVisible().

雨落星ぅ辰 2024-11-16 18:13:17

将滚动条一直移动到底部的一个简单方法是将其值设置为 100,如下所示:

  scroll.getVerticalScrollBar().setValue(100);

这会使其移动到视口的底部。您可以在将组件添加到面板后添加它。

A simple way to move the scrollbar all the way to the bottom is to set its value to 100 like this:

  scroll.getVerticalScrollBar().setValue(100);

This causes it to move to the bottom of the viewport. You can add this after the component is added to the panel.

长梦不多时 2024-11-16 18:13:17

这就是我以编程方式向下滚动的方式。

我喜欢它相当平滑地滚动到底部,而不是立即跳到那里。

/**
 * Scrolls a {@code scrollPane} to its bottom.
 * 
 * @param scrollPane
 *            the scrollPane that we want to scroll all the way down
 * 
 */
private void scrollDown(JScrollPane scrollPane) {
    JScrollBar verticalBar = scrollPane.getVerticalScrollBar();

    int currentScrollValue = verticalBar.getValue();
    int previousScrollValue = -1;

    while (currentScrollValue != previousScrollValue) {
        // Scroll down a bit
        int downDirection = 1;
        int amountToScroll = verticalBar.getUnitIncrement(downDirection);
        verticalBar.setValue(currentScrollValue + amountToScroll);

        previousScrollValue = currentScrollValue;
        currentScrollValue = verticalBar.getValue();
    }
}

This is how I scroll down programmatically.

I like how it scrolls to the bottom rather smoothly instead of jumping there immediately.

/**
 * Scrolls a {@code scrollPane} to its bottom.
 * 
 * @param scrollPane
 *            the scrollPane that we want to scroll all the way down
 * 
 */
private void scrollDown(JScrollPane scrollPane) {
    JScrollBar verticalBar = scrollPane.getVerticalScrollBar();

    int currentScrollValue = verticalBar.getValue();
    int previousScrollValue = -1;

    while (currentScrollValue != previousScrollValue) {
        // Scroll down a bit
        int downDirection = 1;
        int amountToScroll = verticalBar.getUnitIncrement(downDirection);
        verticalBar.setValue(currentScrollValue + amountToScroll);

        previousScrollValue = currentScrollValue;
        currentScrollValue = verticalBar.getValue();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文