import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

    // Display the contents of two files in a vertically split pane.
    // Adjustments of the left pane should move the right pane as well.
class SwingSplitFrame2 extends JFrame {
    public SwingSplitFrame2() {
        final int width = 300, height = 300;
        setSize(width,height);
        Container contents = getContentPane();

        // Create scrollers for each text area.
        final JScrollPane leftScroller = new JScrollPane(getLeft());
        final JScrollPane rightScroller = new JScrollPane(getRight());

        // Listen for adjustments on left vertical scrollbar.
        // Relay them to the right scrollbar.
        leftScroller.getVerticalScrollBar().
                addAdjustmentListener(new AdjustmentListener(){
            public void adjustmentValueChanged(AdjustmentEvent e){
                rightScroller.getVerticalScrollBar().setValue(e.getValue());
            }
        });

        // Add a split pane for document viewing.
        JSplitPane pane = new JSplitPane();
        // Add both scrollers to the split pane.
        pane.setLeftComponent(leftScroller);
        pane.setRightComponent(rightScroller);
        // Set the divider at the mid-point.
        pane.setDividerLocation(0.5);

        contents.add(pane,"Center");

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(new QuitJButton());
        contents.add(buttonPanel,"South");
    }

        // Show the given file in the text area.
    public void showFiles(String file1,String file2){
        try{
            FileReader infile = new FileReader(file1);
            JTextArea left = getLeft();
            left.read(infile,null);
            infile.close();

            infile = new FileReader(file2);
            JTextArea right = getRight();
            right.read(infile,null);
            pack();
        }
        catch(IOException e){
            JOptionPane.showMessageDialog(
                    getContentPane(),
                    e.toString(),
                    "File Failure",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    protected JTextArea getLeft(){
        return left;
    }

    protected JTextArea getRight(){
        return right;
    }

    // Default sizes for the text areas.
    private final int width = 30, height = 10;
    private final JTextArea left = new JTextArea(width,height),
                        right = new JTextArea(width,height);
}
