Situation:
I have a JScrollPane “scrollPane” containing a JPanel “mainPanel” which contains multiple JPanels “itemPanels” one below each other (info read from a database and objects created accordingly).
The user can insert new Entries between the existing ones. In this case the new Entry is added to the database and all components (“itemPanels”) are generated again and added to a new “mainPanel”.
At the end the new “mainPanel” is set to the scrollPane with setViewPortView.
Vector<JPanel> itemPanels = generateMyPanelsFromDB();
JPanel mainPanel = new JPanel(new GridBagLayout());
//.. layout stuff
for(JPanel itemPanel: itemPanels) mainPanel.add(itemPanel,<gridbagconstraintsvar>);
scrollPane.setViewportView(mainPanel);
What happens is that the new components are being added correctly and the scrollpane automatically scrolls to the end.
How can i avoid, tha the scrollpane scrolls to the end (=stays where it was before).
What i tried: Surround the above action with:
int currentPos = scrollPane.getVerticalScrollBar().getValue();
//.. to the setViewPortView stuff
scrollPane.getVerticalScrollBar().setValue(value);
this doesn’t do anything (the scrollpane still scrolls to the end and nothing more happens).
I only managed to rescroll back with the following nesting invokeLater, but this can’t be the right way and also doesn’t prevent the scrolling down and then back up (=hurts the eye)
final int value = scrollPane.getVerticalScrollBar().getValue();
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
scrollPane.setViewportView(mainPanel);
Thread t = new Thread() {
public void run() {
try {
Thread.sleep(1);
}
catch (InterruptedException e) {
e.printStackTrace();
}
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
scrollPane.getVerticalScrollBar().setValue(value);
}
});
}
};
t.start();
}
});
Added 2012-01-14:
public class DebugImportCommandEditPanel extends JPanel {
private static final long serialVersionUID = 1L;
private JScrollPane commandScrollPane;
public DebugImportCommandEditPanel() {
this.setLayout(new BorderLayout());
this.commandScrollPane = new JScrollPane(this.getMainPanel());
this.add(this.commandScrollPane, BorderLayout.CENTER);
}
private JPanel getMainPanel() {
Vector<InnerPanel> innerPanels = new Vector<InnerPanel>();
for(int i=0;i<12;i++) innerPanels.add(new InnerPanel((i+1)+ "."));
JPanel mainPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx=0;c.gridy=0;c.weightx=1;c.weighty=1;c.fill = GridBagConstraints.BOTH;
for(InnerPanel panel: innerPanels) {
mainPanel.add(panel,c);
c.gridy++;
}
return mainPanel;
}
class InnerPanel extends JPanel {
private static final long serialVersionUID = 1L;
protected InnerPanel(String number) {
this.setLayout(new BorderLayout());
this.setBorder(BorderFactory.createTitledBorder("Inner Panel No. "+number));
JTextPane textPane = new JTextPane();
textPane.setText("Lorem ipsum dolor sit amet consectetuer tellus sociis sapien porttitor "+
"Suspendisse. Mattis morbi eu In non ante convallis\n "+
"tempus risus venenatis urna. Sed ipsum et parturient volutpat\n "+
"adipiscing dolor quis adipiscing Donec odio.\n "+
"Neque adipiscing pretium lacus Phasellus neque a vel sed wisi alique\n"+
"t. Condimentum Sed pretium libero vitae facilisi pretium sit consequat a "+
"tincidunt. Pharetra ac Aliquam.");
this.add(textPane,BorderLayout.CENTER);
}
}
}
Seeing your edit: it could be the nested JTextPane – unexpectedly (to me, at least :-), it seems to affect the overall scrolling behaviour even if not the direct child of the viewport.
The way out is to configure the caret policy to not update on setting the text:
Note that you have to set the policy before setting the text.