/**
*
*/
package de.tutorials;
import java.awt.GridLayout;
import java.io.File;
import java.io.FileReader;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JViewport;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
* @author daritho
*
*/
public class LinkedScrollPaneExample extends JFrame {
JScrollPane leftScrollPane;
JTextArea leftTextArea;
JScrollPane rightScrollPane;
JTextArea rightTextArea;
public LinkedScrollPaneExample() {
super("LinkedScrollPaneExample");
setDefaultCloseOperation(EXIT_ON_CLOSE);
leftScrollPane = new JScrollPane(leftTextArea = new JTextArea(25, 20));
rightScrollPane = new JScrollPane(rightTextArea = new JTextArea(25, 20));
try {
File file = new File("c:/gc.txt");
leftTextArea.read(new FileReader(file),"");
rightTextArea.read(new FileReader(file),"");
}catch (Exception e) {
e.printStackTrace();
}
ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JViewport viewport = (JViewport) e.getSource();
Object parent = viewport.getParent();
if (leftScrollPane == parent) {
rightScrollPane.getViewport().setViewPosition(
viewport.getViewPosition());
} else if (rightScrollPane == parent) {
leftScrollPane.getViewport().setViewPosition(
viewport.getViewPosition());
}
}
};
leftScrollPane.getViewport().addChangeListener(changeListener);
rightScrollPane.getViewport().addChangeListener(changeListener);
setLayout(new GridLayout(1, 2));
add(leftScrollPane);
add(rightScrollPane);
pack();
setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
new LinkedScrollPaneExample();
}
}