7 Replies Latest reply on Nov 26, 2002 2:31 PM by 807577

    link to scrollbar

    807577
      Do anybody knows how to coordinate 2 scrollbar ? I mean, I would like that when I move a scrollbar (attached to a Panel), this panel AND another panel (in the same Jframe) move.

      thanks for any help

      chmurb
        • 1. Re: link to scrollbar
          PhHein
          Can you add an adjustmentListener to the first scrollbar and in its adjustmentValueChanged(..) method you set the value for the other scrollbar?
          • 2. Re: link to scrollbar
            kleopatra-JavaNet
            use the same model for both

            Greetings
            Jeanette
            • 3. Re: link to scrollbar
              807577
              I can't catch any Event with the AdjustmentListener
              My source is :

              jsp.getHorizontalScrollBar().addAdjustmentListener(new AdjustmentListener(){
              public void adjustmentValueChanged(AdjustmentEvent ae){
              System.out.println("bouh");
              }
              });


              why ?

              PS : for the model I have tried but my scrollbar disappear
              • 4. Re: link to scrollbar
                807577
                ok finaly it's ok for me.
                thanks
                • 5. Re: link to scrollbar
                  807577
                  If the 2 panels are the same size :
                  import javax.swing.*;
                  import java.awt.*;
                  import java.awt.event.*;
                  import javax.swing.event.*;
                       
                  public class Dtext1 extends JFrame implements ChangeListener
                  {
                       JPanel      textL = new JPanel();
                       JPanel      textR = new JPanel();
                       JScrollPane sL    = new JScrollPane(textL);
                       JScrollPane sR    = new JScrollPane(textR);
                    
                  public Dtext1()
                  {
                       setBounds(20,20,500,400);
                       addWindowListener(new WindowAdapter() 
                       {     public void windowClosing(WindowEvent ev) 
                            {     
                                 System.exit(0); 
                       }});
                       getContentPane().add("West",sL);
                       getContentPane().add("East",sR);
                       textR.setPreferredSize(new Dimension(200,3000));     
                       textL.setPreferredSize(new Dimension(200,3000));          
                       setVisible(true);
                       sL.getViewport().addChangeListener(this);
                       sR.getViewport().addChangeListener(this);
                  }
                  public void stateChanged(ChangeEvent e) 
                  {
                       if (e.getSource() == sL.getViewport())
                       {
                            Point pnt = sL.getViewport().getViewPosition();
                            sR.getViewport().setViewPosition(pnt);
                       }
                       if (e.getSource() == sR.getViewport() )
                       { 
                            Point pnt = sR.getViewport().getViewPosition();
                             sL.getViewport().setViewPosition(pnt);
                       }
                  }
                  
                  public static void main(String[] args)
                  {
                       new Dtext1();
                  }
                  }
                  
                   
                  Noah
                  • 6. Re: link to scrollbar
                    807577
                    thanks
                    • 7. Re: link to scrollbar
                      807577
                      If the panes have different height:
                      import javax.swing.*;
                      import java.awt.*;
                      import java.awt.event.*;
                      import javax.swing.event.*;
                        
                      public class Dtext2 extends JFrame implements ChangeListener
                      {
                           JPanel      textL = new JPanel();
                           JPanel      textR = new JPanel();
                           JScrollPane sL    = new JScrollPane(textL);
                           JScrollPane sR    = new JScrollPane(textR);
                        
                      public Dtext2()
                      {
                           setBounds(20,20,500,400);
                           addWindowListener(new WindowAdapter() 
                           {     public void windowClosing(WindowEvent ev) 
                                {     
                                     System.exit(0); 
                           }});
                           textL.setBackground(Color.pink);
                           textR.setBackground(Color.orange);
                           getContentPane().add("West",sL);
                           getContentPane().add("East",sR);
                           textR.setPreferredSize(new Dimension(200,4000));     
                           textL.setPreferredSize(new Dimension(200,3000));          
                           setVisible(true);
                           sL.getViewport().addChangeListener(this);
                           sR.getViewport().addChangeListener(this);
                      }
                      public void stateChanged(ChangeEvent e) 
                      {
                           if (e.getSource() == sL.getViewport())
                           {
                                Dimension dim = sL.getViewport().getViewSize();
                                Dimension siz = sL.getViewport().getSize(); 
                                Point     pnt = sL.getViewport().getViewPosition();
                                Dimension di2 = sR.getViewport().getViewSize();
                                Point     pn2 = sR.getViewport().getViewPosition();
                                Dimension si2 = sR.getViewport().getSize(); 
                                double d = (double)pnt.y / (double)(dim.height-siz.height) * (double)(di2.height-si2.height);
                                pnt.y    = (int)d;
                                if (Math.abs(pnt.y - pn2.y) > 1) sR.getViewport().setViewPosition(pnt);
                           }
                           if (e.getSource() == sR.getViewport() )
                           {
                                Dimension dim = sR.getViewport().getViewSize();
                                Dimension siz = sR.getViewport().getSize(); 
                                Point     pnt = sR.getViewport().getViewPosition();
                                Dimension di2 = sL.getViewport().getViewSize();
                                Point     pn2 = sL.getViewport().getViewPosition();
                                Dimension si2 = sL.getViewport().getSize(); 
                                double d = (double)pnt.y / (double)(dim.height-siz.height) * (double)(di2.height-si2.height);
                                pnt.y    = (int)d;
                                if (Math.abs(pnt.y - pn2.y) > 1) sL.getViewport().setViewPosition(pnt);
                           }
                      }
                      
                      public static void main(String[] args)
                      {
                           new Dtext2();
                      }
                      }
                      Noah