Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
        }
    });
like image 532
In-young Choung Avatar asked Oct 20 '25 01:10

In-young Choung


2 Answers

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) );
    }
});
like image 191
In-young Choung Avatar answered Oct 21 '25 15:10

In-young Choung


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

like image 43
MadProgrammer Avatar answered Oct 21 '25 14:10

MadProgrammer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!