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); } });
java user-interface swing jpanel jscrollpane
In-young Choung Mar 22 '17 at 19:30 2017-03-22 19:30
source share