How to set scroll to the top - JPanel that has multiple JTextareas inside Jscrollpane

主宰稳场 提交于 2019-12-12 05:52:33

问题


I have a JPanel in a JScrollPane. The JPanel contains multiple JTextAreas vertically.

I like to keep the scroll of the scrollpane to the top whenever the page is refreshed. Currently, the scroll always starts from the bottom.

this is my current code and it doesn't work..

    panel.invalidate();
    panel.revalidate();
    panel.repaint();
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            ((JPanel) panel).setLocation(new Point(0, 0));
        }
    });

I've also tried adding this code below to scrollpane, but it doesn't work..

scrollPanel.getViewport().setViewPosition( new Point(0, 0) );

I've looked into other stackoverflow questions and they use Jtextarea inside Jscrollpane (they solved it using setCaretPosition(0), however I can't use the same function to the panel). In my case, there is an extra layer.

How can I solve this..?

EDIT**

Based on advice from Pavlo Viazovskyy, I've also tried this below and it still doesn't work for me.. :(

    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            scrollPane.getVerticalScrollBar().setValue(0);
        }
    });

回答1:


Thank you very much for all the comments. sorry I didn't give a full proper example in the question as there were too many different classes involved..

In my case, textareas inside Panel inside ScrollPane, I made the scroll to the top by default by using setViewPosition method to scrollPane in the invokelater method.

SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        scrollPane.getViewport().setViewPosition( new Point(0, 0) );
    }
});



回答2:


For when you don't have direct access to the JScrollPane, you can simply use JComponent#scrollRectToVisible

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class ScrollTest {

    public static void main(String[] args) {
        new ScrollTest();
    }

    public ScrollTest() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Test");
                frame.add(new JScrollPane(new BigPane()));
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class BigPane extends JPanel {

        public BigPane() {
            setLayout(new BorderLayout());
            JButton scroll = new JButton("Scroll to top");
            add(scroll, BorderLayout.SOUTH);
            scroll.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    scrollRectToVisible(new Rectangle(0, 0, 1, 1));
                }
            });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

    }

}

Yes, you could walk the component hierarchy till you found a JViewport, but this method does it for you.

Just remember though, the Rectangle is relative to the component which called the method, so if I used the JButton instead, it would try and make the JButton visible, not the panel



来源:https://stackoverflow.com/questions/42961012/how-to-set-scroll-to-the-top-jpanel-that-has-multiple-jtextareas-inside-jscrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!