How to set scroll up - JPanel with multiple JTextareas inside Jscrollpane

I have a JPanel in JScrollPane. JPanel contains several JTextAreas vertically.

I like to keep the scroll scroll at the top whenever the page refreshes. Currently, the scroll always starts from the bottom.

this is my current code and it does not work.

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

I also tried adding this code below to scrollpane, but it does not work.

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

I looked at other issues related to stackoverflow and they use Jtextarea inside Jscrollpane (they solved it with setCaretPosition (0), however I cannot use the same function for the panel). In my case there is an extra layer.

How can I solve this problem ??

EDIT **

Based on the advice of Pavel Vyazovsky, I also tried this below, and it still does not work for me .. :(

  javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { scrollPane.getVerticalScrollBar().setValue(0); } }); 
0
java user-interface swing jpanel jscrollpane
Mar 22 '17 at 19:30
source share
2 answers

Thanks so much for all the comments. it is a pity that I did not give a complete correct example in the question, since there were too many different classes.

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

 SwingUtilities.invokeLater(new Runnable() { @Override public void run() { scrollPane.getViewport().setViewPosition( new Point(0, 0) ); } }); 
+2
Mar 22 '17 at 21:01
source share

If you don't have direct access to JScrollPane , you can just 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 go through the component hierarchy until you find JViewport , but this method does it for you.

Just remember that the Rectangle refers to the component that called this method, so if I used JButton , it would try to make JButton visible, not the panel

+2
Mar 22 '17 at 21:31
source share



All Articles