开发者

Java scroll JScrollPane with JPanel within to bottom

I have a JScrollPane with a very high JPanel inside, that is changed d开发者_StackOverflow中文版ynamically, 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!


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

Java scroll JScrollPane with JPanel within to bottom

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);
            }
        });
    }
}


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().


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.


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();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜