#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 dm1 = src.getViewSize();
    Dimension sz1 = src.getSize();
    Point     pt1 = src.getViewPosition();
    Dimension dm2 = tgt.getViewSize();
    Dimension sz2 = tgt.getSize();
    Point     pt2 = tgt.getViewPosition();
    double d;
    d = pt1.getY()/(dm1.getHeight()-sz1.getHeight())*(dm2.getHeight()-sz2.getHeight());
    pt1.y = (int)d;
    d = pt1.getX()/(dm1.getWidth()-sz1.getWidth())*(dm2.getWidth()-sz2.getWidth());
    pt1.x = (int)d;
    tgt.setViewPosition(pt1);
    adjflg = false;
  }
};