説明:kinmuSrl,orderSrlはScrollPaneです。
以下はソースです。
/**二つテープル一緒にスクロールのためのリスナー*/
ChangeListener c1 = new ChangeListener(){
boolean adjflg = false;
public void stateChanged(ChangeEvent e){
JViewport src = null;
JViewport tgt = null;
if(e.getSource()==kinmuSrl.getViewport()){
src = kinmuSrl.getViewport();
tgt = orderSrl.getViewport();
}else if(e.getSource()==orderSrl.getViewport()){
src = orderSrl.getViewport();
tgt = kinmuSrl.getViewport();
}
if(adjflg || tgt==null || src==null) return;
adjflg = true;
Dimension dim1 = src.getViewSize();
Dimension siz1 = src.getSize();
Point pnt1 = src.getViewPosition();
Dimension dim2 = tgt.getViewSize();
Dimension siz2 = tgt.getSize();
Point pnt2 = tgt.getViewPosition();
double d;
d = pnt1.getY()/(dim1.getHeight()-siz1.getHeight())*(dim2.getHeight()-siz2.getHeight());
pnt1.y = (int)d;
d = pnt1.getX()/(dim1.getWidth()-siz1.getWidth())*(dim2.getWidth()-siz2.getWidth());
pnt1.x = (int)d;
tgt.setViewPosition(pnt1);
adjflg = false;
}
};
使い方:
ScrollPaneにリスナーを付けます。
orderSrl.getViewport().addChangeListener(c1);
kinmuSrl.getViewport().addChangeListener(c1);