Example usage for javax.swing JViewport addChangeListener

List of usage examples for javax.swing JViewport addChangeListener

Introduction

In this page you can find the example usage for javax.swing JViewport addChangeListener.

Prototype

public void addChangeListener(ChangeListener l) 

Source Link

Document

Adds a ChangeListener to the list that is notified each time the view's size, position, or the viewport's extent size has changed.

Usage

From source file:Main.java

public static void synchronizeView(final JViewport masterViewport, final JViewport slaveViewport,
        final int orientation) {
    final ChangeListener c1 = new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            if (masterViewport.getView() == null || slaveViewport.getView() == null) {
                return;
            }/*from  www. j  a v a 2 s.  c  o m*/
            if (orientation == SwingConstants.HORIZONTAL) {
                Point v1 = masterViewport.getViewPosition();
                Point v2 = slaveViewport.getViewPosition();
                if (v1.x != v2.x) {
                    slaveViewport.setViewPosition(new Point(v1.x, v2.y));
                }
            } else if (orientation == SwingConstants.VERTICAL) {
                Point v1 = masterViewport.getViewPosition();
                Point v2 = slaveViewport.getViewPosition();
                if (v1.y != v2.y) {
                    slaveViewport.setViewPosition(new Point(v2.x, v1.y));
                }
            }
        }
    };

    masterViewport.addChangeListener(c1);
}