#navi(contents-page-name): No such page: ST

FrontPage>Swing Tips>ST/SyncScroll

ST/SyncScroll

Terai Atsuhiro 2022-01-25 (火) 22:39:54

http://terai.s55.xrea.com/swing/syncscroll/screenshot.png

ChangeListener cl = new ChangeListener(){
  boolean adjflg = false;
  public void stateChanged(ChangeEvent e){
    JViewport src = null;
    JViewport tgt = null;
    if(e.getSource()==sp1.getViewport()){
      src = sp1.getViewport();
      tgt = sp2.getViewport();
    }else if(e.getSource()==sp2.getViewport()){
      src = sp2.getViewport();
      tgt = sp1.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;
  }
};