Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9139119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:20:46+00:00 2026-06-17T09:20:46+00:00

Situation: I have a JScrollPane scrollPane containing a JPanel mainPanel which contains multiple JPanel

  • 0

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);
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:20:47+00:00Added an answer on June 17, 2026 at 9:20 am

    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:

    JTextPane tp = new JTextPane();
    DefaultCaret caret = (DefaultCaret) tp.getCaret();
    caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
    textPane.setText("Lorem ipsum dolor sit amet consectetuer tellus sociis sapien porttitor "+
        ....
    

    Note that you have to set the policy before setting the text.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a situation like this. I have scrollpane whose viewportView is a JPanel
Situation I have 5 Access DB files, each one has 10 tables, 40 queries
Situation: I have several Gridviews on one page, and each Gridview has a dynamically
Situation: I have a database that contains sales records, each record has a ID
I have the following situation I have one swt class which displays a simple
I have situation where there are multiple test cases which uses a common method.
I have following situation - Have a MongoService class, which reads host, port, database
Situation: I have an index page which loads pages into it with ajax as
Situation I have a QList<QVariant> where each QVariant item should be a QList<QVariant> of
Situation : I have a Dialog class in QT on which I draw a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.