How to communicate between two components in both direction?

On my applet, I have a 200*200 canvas (say CanvasA), and a scrollPane contains another 2000*2000 canvas (say CanvasB) on it.  Now I want to two direction communication like this:
1. when I click on CanvasA's position (x, y), I want the scrollPane's scroll position to be adjusted to (10*x, 10*y).
I can implement this by put the scrollPane in CanvasA's constructor;
2. however, when I drag or click on scrollPane's scroll bar, I can get a scroll position (x, y) form scrollPane, I also want a rectangle defined by (x/10, y/10, 30, 30) reflected on CanvasA.

also, here applet, CanvasA, CanvasB are three different class, while scrollPane is just a component on applet.

Hope I have made myself clear.

Looking for your answer, thanks,

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

you cannot add a listener to the scrollbars in a scrollpane.
either you can go in for Scrollbar or JScrollPane.

wud swing be ok? or r u particulat abt java 1.1x??
DaisyWangAuthor Commented:
To: sqoms

I mean I have successfully add a listener to scrollpane (not scrollbar).  However, what I want is whenever I click on the scrollbar of scrollPane, I want the CanvasA to reflect the proper area of CanvasB shown on scrollPane.


'applet, CanvasA, CanvasB are three different class, while scrollPane is just a component on applet.'

do you have problem in implementing some functionality or you just can't access some object methods from other objects ?

generally speakig, you can always use these methods

(maybe it's better to post your code ?)
Yes DaisyWang,

you can add a Listener to a Scrollpane. sorry abt that.
chk out this program,

import java.awt.*;
import java.awt.event.*;

public class Canvas_Pane extends java.applet.Applet{
      public void init(){
        setLayout(new BorderLayout());
            final ScrollPane pane=new ScrollPane();
        final canvasA canA=new canvasA(pane);
             canvasB canB=new canvasB();
            pane.getHAdjustable().addAdjustmentListener(new AdjustmentListener(){
               public void adjustmentValueChanged(AdjustmentEvent e){
                               canA.draw(pane.getScrollPosition().x, pane.getScrollPosition().y);
            pane.getVAdjustable().addAdjustmentListener(new AdjustmentListener(){
               public void adjustmentValueChanged(AdjustmentEvent e){
                         canA.draw(pane.getScrollPosition().x, pane.getScrollPosition().y);
class canvasA extends Canvas{
   int x=-1,y=-1;
       ScrollPane scroll;
       canvasA(ScrollPane pane){
             addMouseListener(new MouseAdapter(){
                    public void mouseClicked(MouseEvent e){
                            scroll.setScrollPosition(e.getX()*10, e.getY()*10);
       public void draw(int xPos,int yPos){
             x=xPos; y=yPos;
       public void paint(Graphics g){
              if(x!=-1 && y!=-1 ){
class canvasB extends Canvas{

will this do?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DaisyWangAuthor Commented:
ya, it works!  thank you a lot!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.