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?

[Webinar] Streamline your web hosting managementRegister Today

sgomsConnect With a Mentor Commented:
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?
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 ?)
DaisyWangAuthor Commented:
ya, it works!  thank you a lot!
All Courses

From novice to tech pro — start learning today.