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

  • SEARCH
  • Home
  • 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 6051661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:50:35+00:00 2026-05-23T07:50:35+00:00

I am a Swing novice using NetBeans. I want to vertically scroll two side-by-side

  • 0

I am a Swing novice using NetBeans. I want to vertically scroll two side-by-side JTextPane’s. The scrolling should be syncronized and done through a single scrollbar.

If I add JTextPanes from the NetBean designer they are automatically put inside a JScrollPane so they scroll independently. I have deleted the enclosing scroll panes and put them in a JPanel instead so the JPanel can be the client of a single JScrollPane. This seems to work except that when the JTextPanes are very long they seem to go beyong the end of the JPanel. I can scroll the panel and both text panes to a certain point. When I reach the bottom I can put a cusor in one of the text panes and arrow down beyond my field of view.

Here is code from my main method. I have copied the layout from something that was generated by the NetBeans designer.

java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
topFrame aTopFrame = new topFrame();
JScrollPane scollBothDiffs = new javax.swing.JScrollPane();
JPanel bothDiffsPanel = new javax.swing.JPanel();
JTextPane leftDiffPane = diffPane1;
JTextPane rightDiffPane = diffPane2;


javax.swing.GroupLayout bothDiffsPanelLayout = new javax.swing.GroupLayout(bothDiffsPanel);
bothDiffsPanel.setLayout(bothDiffsPanelLayout);
bothDiffsPanelLayout.setHorizontalGroup(
    bothDiffsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(bothDiffsPanelLayout.createSequentialGroup()
        .addGap(20, 20, 20)
        .addComponent(leftDiffPane, javax.swing.GroupLayout.PREFERRED_SIZE, 463, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(rightDiffPane, javax.swing.GroupLayout.PREFERRED_SIZE, 463, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(52, Short.MAX_VALUE))
);
bothDiffsPanelLayout.setVerticalGroup(
    bothDiffsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, bothDiffsPanelLayout.createSequentialGroup()
        .addContainerGap()
        .addGroup(bothDiffsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
            .addComponent(rightDiffPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 630, Short.MAX_VALUE)
            .addComponent(leftDiffPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 630, Short.MAX_VALUE))
        .addContainerGap())
);

scollBothDiffs.setViewportView(bothDiffsPanel);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(aTopFrame.getContentPane());
aTopFrame.getContentPane().setLayout(layout);
layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
        .addGap(20, 20, 20)
        .addComponent(scollBothDiffs, javax.swing.GroupLayout.DEFAULT_SIZE, 997, Short.MAX_VALUE)
        .addContainerGap())
);
layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(scollBothDiffs, javax.swing.GroupLayout.DEFAULT_SIZE, 671, Short.MAX_VALUE)
        .addContainerGap())
);

aTopFrame.pack();
aTopFrame.setVisible(true);
}
});

Here is an image that shows my implementation of the first answer where the text panes are not limited to the horizontal display area.
Image showing text panes that are too wide for the horizontal display area

And this image shows the text panes limited in the horizontal display area but this example has the original issue of not scrolling vertically if the text panes are very long.
Image showing text panes that are bounded horizontally

  • 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-05-23T07:50:35+00:00Added an answer on May 23, 2026 at 7:50 am

    A trick I’ve used before is to use two JScrollPanes, hide the vertical scroll bar for the left pane, and set its model to the model for the right scroll pane. That way, when the user scrolls the right scroll bar, it updates both scroll panes, since they share the same model. Here is an example that also has dual synchronized horizontal scroll bars at the bottom:

    JTextPane leftDiffPane = diffPane1;
    JTextPane rightDiffPane = diffPane2;
    JScrollPane spLeft = new JScrollPane(leftDiffPane, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    JScrollPane spRight = new JScrollPane(leftDiffPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    spLeft.getHorizontalScrollBar().setModel(spRight.getHorizontalScrollBar().getModel());
    spLeft.getVerticalScrollBar().setModel(spRight.getVerticalScrollBar().getModel());
    
    // ...
    

    Note that it might not work so well if your JTextPanes have different sizes (I haven’t tried it, but my best guess is it would cause problems)

    You can also fix mouse wheel scrolling by redirecting wheel events from the left scroll pane to the right one:

    spLeft.setWheelScrollingEnabled(false);
    spLeft.addMouseWheelListener(new MouseWheelListener() {
        public void mouseWheelMoved(MouseWheelEvent e) {
            spRight.dispatchEvent(e);
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Building a client-side swing application what should be notified on a bus (application-wide message
Using Swing in Java I wrote a form containing radio buttons, text fields and
Is double buffering (in java) possible with awt? Currently, I'm aware that swing should
My Swing Application's GUI is built using Window Builder Pro GUI editor. Layouts used
In Swing when using a JTree/JList/JTable selecting an item changes its background color. Is
I am using Google Web Toolkit's Swing Designer in Eclipse to create and edit
I have a Java Swing UI that isn't updating/repainting as I thought it should.
In Swing, I am able to prompt the user for input using JOptionPane .
I'm new to Jackrabbit and I'm using Sling to access the repository through its
In Swing, using Grid Layout, each grid has same size.. is it possible to

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.