• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1478
  • Last Modified:

Synchronizing vertical scrollbars

I have two JTables (each is contained in a JScrollPane).  The JTable on the left is fixed and I do not want to show the scrollbars, but the JTable on the right is not fixed and the scrollbars show if they are needed.  I want to be able to synch up the left JTable's vertical scrollbar with that of the right JTable's vertical scrollbar so that when the right table is scrolled vertically, the left table scrolls with it.  What is the best way to do this?  (Note:  keep in mind that the left table will never show a horizontal scrollbar but the right table may have a horizontal scrollbar if one is needed.)
1 Solution
I think this does most of what you're looking for.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ScrollFrame
      extends JFrame
      implements AdjustmentListener
      JScrollPane left = null;
      JScrollPane right = null;
      JScrollBar rightHorizontal = null;
      JScrollBar rightVertical = null;

      JScrollBar leftHorizontal = null;
      JScrollBar leftVertical = null;
      public static void main(String args[])
            JFrame f = new ScrollFrame();
      public ScrollFrame()
            JTextArea leftArea = new JTextArea(200, 200);
            JTextArea rightArea = new JTextArea(200, 200);
            left = new JScrollPane(leftArea);
            right = new JScrollPane(rightArea,
            leftHorizontal = left.getHorizontalScrollBar();
            leftVertical = left.getVerticalScrollBar();

            rightHorizontal = right.getHorizontalScrollBar();
            rightVertical = right.getVerticalScrollBar();


            Box box = Box.createHorizontalBox();
            setSize(400, 200);
      public void adjustmentValueChanged(AdjustmentEvent e)
            if (e.getSource() == leftHorizontal)
            else if (e.getSource() == leftVertical)


bjgAuthor Commented:
This will not do what I am looking for because the problem does not lie within when the right scrollpane's horizontal scrollbar is being scrolled, but the vertical scrollbar.  

If the right horizontal scrollbar is visible while scrolling the right vertical scrollbar, the rows do not always line up between the left table and the right table because the visible viewports are different sizes (because the right table's viewport has a horizontal scrollbar showing, but the left table's viewport does not show a horizontal scrollbar).  Therefore, I have to somehow look for this situation and correct it.  How can I do this?
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.  Or if you are an EE Pro user, click the link below to select open items for your Member ID using Power Search:

This is the Community Support link, if help is needed, along with the link to All Topics which reflects many TAs recently added.

Thank you,
Moderator @ Experts Exchange
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now