Java Usage of JScrollPane

What I have below is an annotated listing of my "second(?)" success. Part of the code is stolen from SUN's jswing documentation and tutorials. In general, what is shown in blue is stuff generated by the IDE (not completely, however), and what is shown in black is my typing (I hope you know what I mean). I will annotate the text in color comments which should be self evident as to whether they are code or comments.
 
/*
 * ScrollTest.java
 *
 * Created on August 4, 2003, 11:01 AM
 * "finalized August 6, 2003, but note that it took hours and hours to
 * find the setPreferred... required.
 * Note also that some text stolen from other sources, including SUN,
 * without attribution (lost?).
 */

/**
 *
 * @author  Carl W. David
 */

//The following imports were demanded by various calls,
//but could have been avoided if one was willing to write something like
//java.awt.event.WindowListener rather than just WindowListener in the implements section (below).

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


public class ScrollTest extends javax.swing.JFrame implements WindowListener,ActionListener{

//I had to add the "implements WindowListener" after the fact, since
// the IDE added a WindowListener (see below), and the program wouldn't compile
// after that!

    private MyPanel drawingPanel;
    private Container contentPane;
    
    // public int ox, oy, x, y;
    
    /** Creates new form ScrollTest */
    public ScrollTest() {
        initComponents();
        setSize(400,400);

//Here, the drawingPanel is set larger than the size of the JFrame which
//contains it.

        drawingPanel = new MyPanel(1000,1000)i;

//Here, the fact that the thing to be drawn is bigger than the enclosing
//JScrollPane is made apparent to the enclosing JScrollPane.

        drawingPanel.setPreferredSize(new Dimension(1000,1000));
        jScrollPane1.setViewportView(drawingPanel);

//Here the Listener is attuned to the Scrolling Events both Horizontal
//and Vertical

        AdjustmentListener listener = new MyAdjustmentListener();
        jScrollPane1.getHorizontalScrollBar().addAdjustmentListener(listener);
        jScrollPane1.getVerticalScrollBar().addAdjustmentListener(listener);

//and we're done.

    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */

    private void initComponents() {//GEN-BEGIN:initComponents
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();

        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.X_AXIS));

//Here is the WindowListener that the IDE added

        addWindowListener(this);

        jButton1.setText("press to redraw");
        jButton1.addActionListener(this);

        getContentPane().add(jButton1);

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        getContentPane().add(jScrollPane1);

        pack();
    }

    // Code for dispatching events from components to event handlers.

    public void actionPerformed(java.awt.event.ActionEvent evt) {
        if (evt.getSource() == jButton1) {
            ScrollTest.this.jButton1ActionPerformed(evt);
        }
    }

    public void windowActivated(java.awt.event.WindowEvent evt) {
    }

    public void windowClosed(java.awt.event.WindowEvent evt) {
    }

    public void windowClosing(java.awt.event.WindowEvent evt) {
        if (evt.getSource() == ScrollTest.this) {
            ScrollTest.this.exitForm(evt);
        }
    }

    public void windowDeactivated(java.awt.event.WindowEvent evt) {
    }

    public void windowDeiconified(java.awt.event.WindowEvent evt) {
    }

    public void windowIconified(java.awt.event.WindowEvent evt) {
    }

    public void windowOpened(java.awt.event.WindowEvent evt) {
    }//GEN-END:initComponents
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // Add your handling code here:
        jScrollPane1.revalidate();
    }//GEN-LAST:event_jButton1ActionPerformed
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        System.exit(0);
    }//GEN-LAST:event_exitForm
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new ScrollTest().show();
    }
    
    public void adjustmentValueChanged(AdjustmentEvent e) {
        System.out.println("adjustmentValueChanged");
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables


}

//the following was literally taken from the SUN jswing tutorial
//except for some printing


class MyAdjustmentListener implements AdjustmentListener {
    // This method is called whenever the value of a scrollbar is changed,
    // either by the user or programmatically.
    public void adjustmentValueChanged(AdjustmentEvent evt) {
        Adjustable source = evt.getAdjustable();
        System.out.println("MyAdjustmentListener(AdjustmentListener)");
        // getValueIsAdjusting() returns true if the user is currently
        // dragging the scrollbar's knob and has not picked a final value
        if (evt.getValueIsAdjusting()) {
            // The user is dragging the knob
            System.out.println("The user is using the knob");
            return;
        }
        
        // Determine which scrollbar fired the event
        int orient = source.getOrientation();
        if (orient == Adjustable.HORIZONTAL) {
            // Event from horizontal scrollbar
        } else {
            // Event from vertical scrollbar
        }
        
        // Determine the type of event
        int type = evt.getAdjustmentType();
        System.out.println("type = "+type);
        switch (type) {
            case AdjustmentEvent.UNIT_INCREMENT:
                // Scrollbar was increased by one unit
                break;
            case AdjustmentEvent.UNIT_DECREMENT:
                // Scrollbar was decreased by one unit
                break;
            case AdjustmentEvent.BLOCK_INCREMENT:
                // Scrollbar was increased by one block
                break;
            case AdjustmentEvent.BLOCK_DECREMENT:
                // Scrollbar was decreased by one block
                break;
            case AdjustmentEvent.TRACK:
                // The knob on the scrollbar was dragged
                break;
        }
        
        // Get current value
        int value = evt.getValue();
    }
}


//That's the end of the stuff from SUN.
//What I learned (below) is that one needs to create a JPanel, or Canvas, or 
//something, WHOSE PAINT METHOD ADDRESSES just that part(!), which is why we have
to do it!


class MyPanel  extends JPanel{
    
    protected MyPanel(int width, int height){
        this.setSize(width,height);
    }//end of protected MyPanel
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        // Retrieve the graphics context; this object is used to paint shapes
        Graphics2D g2d = (Graphics2D)g;
        // Draw an oval that fills the window
        

        int width = this.getBounds().width;
        int height = this.getBounds().height;
        
        
        g2d.drawOval(10,20, 300,400);
        // and Draw a diagonal line that fills MyPanel
        g2d.drawLine(0,0,width,height);
    }//end of paint
}//end of class MyPanel

Return to the main book TOC.