3 Replies Latest reply on Mar 29, 2004 2:49 PM by 807587

    Synchronizing scrollbars two scroll bars

    807577
      I have a JDialog in my application that contains a JPanel with two JTables in JScrollPanes. The effect I am looking for is if I slide the scroll bar in the left ScrollPane, the right one also scrolls and vica-versa. Can anyone provide some guidance on how to do this?

      Thanks
        • 1. Re: Synchronizing scrollbars two scroll bars
          807577
          Use a java.awt.event.AdjustmentListener
          • 2. Re: Synchronizing scrollbars two scroll bars
            807577
            I used ChangeListener like that:

                 /** Implements ChangeListener to synchronize the two scrollpanes sp1, sp2. */
                 public void stateChanged(ChangeEvent e)     {
                      JViewport src = null, tgt = null;
                      if (e.getSource() == sp1.getViewport())     {
                           src = sp1.getViewport();
                           tgt = sp2.getViewport();
                      }
                      else
                      if (e.getSource() == sp2.getViewport())     {
                           src = sp2.getViewport();
                           tgt = sp1.getViewport();
                      }
                           
                      if (tgt != null && src != null)     {
                           // set position of peer view
                           tgt.removeChangeListener(this);
                           
                           tgt.setViewPosition(src.getViewPosition());
                           
                           tgt.addChangeListener(this);
                      }
                 }
            • 3. Re: Synchronizing scrollbars two scroll bars
              807587
              Where do I have to put this method??
              Please help!