Java Usage of JScrollPane on Text Rather Than Graphics

The first two examples of ScrollPane usage concerned graphics, but I wanted to do something with text also, and at the same time I wanted to use Adam Doppelt's DebugLog program, which I found more helpful than inserting System.out.println text everywhere. I thought that I could alter Doppelt's code to return immediately if a flag had been set (not in this code), to turn off debugging easily.
So here is an example which contains DebugLog2.java, a program which can dump variables to a JFrame and save that text (for possible printing).
/*
 * DebugLog2.java
 *
 * Created on December 15, 2004, 1:19 PM
 * Usage 
 *          DebugLog2 debuglog2 = new DebugLog2();
 *          debuglog2.println("seme text "+variable);
 */

package vcpm1a;
import javax.swing.*;
import java.awt.*;
import java.io.*;

/**
 *
 *   c. w. david from Adam Doppelt's code
 */
public class DebugLog2 extends javax.swing.JFrame {
    
    /** Creates new form DebugLog2 */
    public DebugLog2() {
        initComponents();
        
        area_ = new JTextArea(ROWS,COLS);
        area_.setEditable(true);
        setLayout(new FlowLayout());
        add(area_);
        setVisible(true);
        
        Dimension size = area_.getPreferredSize();
        area_.setSize(size);
        size.width += 30;
        size.height += 60;
        //       getSize(size);
        setSize(size);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        area_ = new javax.swing.JTextArea();
        
        getContentPane().setLayout(new java.awt.GridBagLayout());
        java.awt.GridBagConstraints gridBagConstraints1;
        
        area_.setColumns(80);
        area_.setRows(80);
        jScrollPane1.setViewportView(area_);
        
        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = 1;
        gridBagConstraints1.gridwidth = 3;
        gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints1.insets = new java.awt.Insets(4, 4, 4, 4);
        gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints1.weightx = 1.0;
        gridBagConstraints1.weighty = 1.0;
        getContentPane().add(jScrollPane1, gridBagConstraints1);
        
    }
    
    /** 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
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();

        getContentPane().setLayout(new java.awt.GridBagLayout());

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jButton2.setText("save");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton2, new java.awt.GridBagConstraints());

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setViewportBorder(new javax.swing.border.TitledBorder("jScrollPane1.Viewport"));
        jScrollPane1.setAutoscrolls(true);
        jScrollPane1.setMinimumSize(new java.awt.Dimension(140, 140));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(600, 600));
        getContentPane().add(jScrollPane1, new java.awt.GridBagConstraints());

        pack();
    }//GEN-END:initComponents
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        // TODO add your handling code here:
        try {
            FileOutputStream outFile = new FileOutputStream("c:/Documents and Settings/david/DESKTOP/DebugLogOutput.txt");
            
            DataOutputStream outData = new DataOutputStream(outFile);
            
            String myList = area_.getText();
            String here = null;
            here = "End Of File";
            
            outData.writeUTF(myList);
            
            outData.writeUTF(here);
            outData.close();
            
        } catch (Exception e) {
            e.printStackTrace();
        }

    }//GEN-LAST:event_jButton2ActionPerformed
    
    /**
     *  args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DebugLog2().setVisible(true);
                
            }
        });
        
    }
    public void print(String s){
        area_.setText(area_.getText()+s);
    }
    public void println(String s){
        print(s+"
");
    }
    public void println(){
        print("
");
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    final private static int ROWS=25;
    final private static int COLS = 80;
    public JTextArea area_ = null;
}

Return to the main book TOC.