import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;
/**
* This class demonstrates how to synchronize the adjustment value of the many
* <code>JScrollPane</code>s.
*
* @author Willy Michel
*/
public class SynchronizedPanesDemo extends JFrame {
/**
* Creates a new instance of <code>SynchronizedPanesDemo</code>.
*/
public SynchronizedPanesDemo() {
super("SynchronizedPanes Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTextPane textPaneLeft = new JTextPane();
fillTextPane(textPaneLeft, "Left TextPane");
final JTextPane textPaneRight = new JTextPane();
fillTextPane(textPaneRight, "Right TextPane");
final JScrollPane scrollPaneLeft = new JScrollPane(textPaneLeft);
final JScrollPane scrollPaneRight = new JScrollPane(textPaneRight);
final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
scrollPaneLeft, scrollPaneRight);
getContentPane().add(splitPane);
final AdjustmentController controller = new AdjustmentController();
controller.registerScrollPane(scrollPaneLeft);
controller.registerScrollPane(scrollPaneRight);
setSize(400, 300);
centerFrame(this);
setVisible(true);
splitPane.setDividerLocation(0.5);
}
/**
* Fills the given TextPane with some number of rows with the specified
* <code>feed</code>.
*
* @param pane A <code>JTextPane</code> to fill.
* @param feed A string to set to the rows.
*/
private static void fillTextPane(final JTextPane pane, final String feed) {
final StringBuffer text = new StringBuffer();
for (int i = 0; i < 50; i++) {
text.append(feed);
text.append("\n");
}
pane.setText(text.toString());
}
/**
* Locate the given Frame on the center of the screen.
*
* @param frame A <code>JFrame</code> to center.
*/
private static void centerFrame(final JFrame frame) {
final Dimension frameSize = frame.getSize();
final Toolkit toolkit = Toolkit.getDefaultToolkit();
final Dimension screenSize = toolkit.getScreenSize();
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
}
/**
* Starts the application.
*
* @param args Programm arguments
*/
public static void main(String[] args) {
new SynchronizedPanesDemo();
}
/**
* The <code>AdjustmentController</code> synchronizes the adjustment value
* of the ScrollBars of all registered <code>JScrollPane</code>s.
*/
private class AdjustmentController implements AdjustmentListener {
private List horizontalBars;
private List verticalBars;
/**
* Creates a new instance of <code>AdjustmentController</code>.
*/
public AdjustmentController() {
super();
horizontalBars = new ArrayList();
verticalBars = new ArrayList();
}
/**
* Adds the both <code>JScrollBar</code>s of the given <code>scrollPane
* </code> to the lists of horizontal and vertical bars. Adds this
* controller as an <code>AdjustmentListener</code> to the bars.
*
* @param scrollPane a <code>JScrollPane</code> to register.
*/
public final void registerScrollPane(final JScrollPane scrollPane) {
JScrollBar scrollBar = scrollPane.getHorizontalScrollBar();
scrollBar.addAdjustmentListener(this);
horizontalBars.add(scrollBar);
scrollBar = scrollPane.getVerticalScrollBar();
scrollBar.addAdjustmentListener(this);
verticalBars.add(scrollBar);
}
/* (non-Javadoc)
* @see java.awt.event.AdjustmentListener#adjustmentValueChanged(
* java.awt.event.AdjustmentEvent)
*/
public final void adjustmentValueChanged(final AdjustmentEvent e) {
final JScrollBar scrollBar = (JScrollBar) e.getAdjustable();
if (verticalBars.contains(scrollBar)) {
synchronizeScrollBars(verticalBars, e.getValue());
} else if (horizontalBars.contains(scrollBar)) {
synchronizeScrollBars(horizontalBars, e.getValue());
}
}
/**
* Sets the adjustment value of all ScrollBars in the specified list
* to the given value.
*
* @param bars A <code>List</code> of the <code>JScrollBar</code>s.
* @param value The adjustment value to set.
*/
private void synchronizeScrollBars(final List bars, final int value) {
JScrollBar bar;
for (int i = 0; i < bars.size(); i++) {
bar = (JScrollBar) bars.get(i);
bar.setValue(value);
}
}
}
}