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,

Daisy
DaisyWangAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
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);
            add("North",canA);
            
             canvasB canB=new canvasB();
            pane.add(canB);
            add("Center",pane);
            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);
                        }
            });
            resize(300,300);
      }
}
class canvasA extends Canvas{
   int x=-1,y=-1;
       ScrollPane scroll;
       canvasA(ScrollPane pane){
             scroll=pane;
             setSize(200,200);
             setBackground(Color.lightGray);
             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;
             repaint();
       }
       public void paint(Graphics g){
              if(x!=-1 && y!=-1 ){
                    g.fillRect(x/10,y/10,30,30);
                    x=-1;y=-1;
                  }
       }
                  
}
class canvasB extends Canvas{
  canvasB(){
        setSize(2000,2000);
            setBackground(Color.gray);
      }
}


will this do?
-sgoms
0
 
sgomsCommented:
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??
0
 
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.

Thanks.

Daisy
0
 
heyhey_Commented:
'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
addAdjustmentListener()
setScrollPosition()

(maybe it's better to post your code ?)
0
 
DaisyWangAuthor Commented:
ya, it works!  thank you a lot!
0
All Courses

From novice to tech pro — start learning today.