Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Synchronizing vertical scrollbars

Posted on 1999-06-29
3
Medium Priority
?
1,471 Views
Last Modified: 2008-03-03
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.)
0
Comment
Question by:bjg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 2

Accepted Solution

by:
samnelson earned 3200 total points
ID: 1244782
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();
            f.show();
      }
      
      public ScrollFrame()
      {
            JTextArea leftArea = new JTextArea(200, 200);
            JTextArea rightArea = new JTextArea(200, 200);
            
            left = new JScrollPane(leftArea);
            right = new JScrollPane(rightArea,
                                                ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
                                                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            
            leftHorizontal = left.getHorizontalScrollBar();
            leftVertical = left.getVerticalScrollBar();

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

            leftHorizontal.addAdjustmentListener(this);
            leftVertical.addAdjustmentListener(this);

            Box box = Box.createHorizontalBox();
            box.add(left);
            box.add(right);
            
            getContentPane().add(box);
            setTitle("ScrollGuy");
            pack();
            setSize(400, 200);
      }
      
      public void adjustmentValueChanged(AdjustmentEvent e)
      {
            if (e.getSource() == leftHorizontal)
                  rightHorizontal.setValue(e.getValue());
            else if (e.getSource() == leftVertical)
                  rightVertical.setValue(e.getValue());
            else
                  System.out.println("Huh?");

            right.updateUI();
      }
            
}

0
 

Author Comment

by:bjg
ID: 1244783
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?
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6827483
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:
http://www.experts-exchange.com/jsp/qPowerSearch.jsp.

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

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
http://www.experts-exchange.com/jsp/zonesAll.jsp
 
Thank you,
Moondancer
Moderator @ Experts Exchange
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question