• Support PF! Buy your school textbooks, materials and every day products Here!

Java GUI output to text file

  • Comp Sci
  • Thread starter iamjon.smith
  • Start date
  • #26
117
3
Working on getting the indentation corrected, here is the code as it stands now.
(minus the CityRecord class which seems to have correct indentation)
Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PopulationForm.java
 *
 * Created on Feb 14, 2011, 12:28:57 PM
 */

package phase4dbpopulation;

import java.io.*;
import java.util.*;
/**
 *
 * @author Jon and Jessica
 */
public class PopulationForm extends javax.swing.JFrame {

    /** Creates new form PopulationForm */
    public PopulationForm() {
        initComponents();
    }

    /** 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        city = new javax.swing.JTextField();
        state = new javax.swing.JTextField();
        population = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        clearFormButton = new javax.swing.JButton();
        quitButton = new javax.swing.JButton();
        printToFileButton = new javax.swing.JButton();
        enterDataButton = new javax.swing.JButton();

        jLabel1.setText("jLabel1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel2.setText("Population Form");

        city.setNextFocusableComponent(state);

        state.setNextFocusableComponent(population);

        jLabel3.setText("City:");

        jLabel4.setText("State:");

        jLabel5.setText("Population");

        clearFormButton.setText("Clear Form");
        clearFormButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearFormButtonActionPerformed(evt);
            }
        });

        quitButton.setText("QUIT");
        quitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitButtonActionPerformed(evt);
            }
        });

        printToFileButton.setText("Print to File");
        printToFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                printToFileButtonActionPerformed(evt);
            }
        });

        enterDataButton.setText("Enter Data");
        enterDataButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                enterDataButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(city, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
                    .addComponent(state)
                    .addComponent(population))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 157, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(clearFormButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(quitButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(printToFileButton))
                .addGap(58, 58, 58))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(233, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addGap(212, 212, 212))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(168, Short.MAX_VALUE)
                .addComponent(enterDataButton, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(139, 139, 139))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(city, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(clearFormButton))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(state, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(quitButton))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(population, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(printToFileButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addComponent(enterDataButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    private void clearFormButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // When Clear Button is pressed, clear all text fields
        city.setText("");
        state.setText("");
        population.setText("");

    }                                        

    private void quitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // When EXIT Button is pressed, exit application
        System.exit(0);
    }                                        

    private void printToFileButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // When PRINT TO FILE Button is pressed, print all entered data to file population.txt
        FileOutputStream out; // declare a file output object
        PrintStream p; // declare a print stream object

        try
        {
             // Create a new file output stream
             // connected to "population.txt"
             out = new FileOutputStream("population.txt");

             // Connect print stream to the output stream
             p = new PrintStream( out );

             p.println ("Population by City and State");
             p.println("----------------------------");
             for (CityRecord rec : cityRecordList) 
             {
                p.println(rec);

             }


                p.close();
         }
         catch (Exception e)
         {
                        System.err.println ("Error writing to file");
         }


    }                                        

    private void enterDataButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        
        String cityName;
        String stateName;
        int populationNum;
        cityName = city.getText();
        stateName = state.getText();
        populationNum = Integer.parseInt(population.getText());

        // create a CityRecord object
        CityRecord cityRecord = new CityRecord(cityName, stateName, populationNum);

        // Add elements to array list
        cityRecordList.add(cityRecord);

        city.setText("");
        state.setText("");
        population.setText("");
    }                                        

    // Create an array list that can grow as data is input by user
    ArrayList<CityRecord> cityRecordList = new ArrayList<CityRecord>();
   
    /**
    * @param args the command line arguments
    */


    public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PopulationForm().setVisible(true);
            }
        });

    }

    // Variables declaration - do not modify
    private javax.swing.JTextField city;
    private javax.swing.JButton clearFormButton;
    private javax.swing.JButton enterDataButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField population;
    private javax.swing.JButton printToFileButton;
    private javax.swing.JButton quitButton;
    private javax.swing.JTextField state;
    // End of variables declaration

}
As for commenting, I actually have to go back and add more comments than what is there, "Planning Comments" is what the instructor calls them. I just throw the extra *coughcrapcough* comments in there when I am done so it passes inspections.

As for the IDE, I am using netBeans 6.7.1 (older version so it has working UML Diagram Tool). I am trying to see if there is some built in validation I can use for the text fields.
 
  • #27
33,088
4,793
Much improved!
 
  • #28
297
2
Indentation is mostly good. Just a few stray lines and extraneous blank lines I'd take care of. Keeping the general indentation style, I'd have it look something like this:
Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PopulationForm.java
 *
 * Created on Feb 14, 2011, 12:28:57 PM
 */

package phase4dbpopulation;

import java.io.*;
import java.util.*;

/**
 *
 * @author Jon and Jessica
 */
public class PopulationForm extends javax.swing.JFrame {

    /** Creates new form PopulationForm */
    public PopulationForm() {
        initComponents();
    }

    /** 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        city = new javax.swing.JTextField();
        state = new javax.swing.JTextField();
        population = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        clearFormButton = new javax.swing.JButton();
        quitButton = new javax.swing.JButton();
        printToFileButton = new javax.swing.JButton();
        enterDataButton = new javax.swing.JButton();

        jLabel1.setText("jLabel1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel2.setText("Population Form");

        city.setNextFocusableComponent(state);

        state.setNextFocusableComponent(population);

        jLabel3.setText("City:");

        jLabel4.setText("State:");

        jLabel5.setText("Population");

        clearFormButton.setText("Clear Form");
        clearFormButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearFormButtonActionPerformed(evt);
            }
        });

        quitButton.setText("QUIT");
        quitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitButtonActionPerformed(evt);
            }
        });

        printToFileButton.setText("Print to File");
        printToFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                printToFileButtonActionPerformed(evt);
            }
        });

        enterDataButton.setText("Enter Data");
        enterDataButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                enterDataButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(city, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
                    .addComponent(state)
                    .addComponent(population))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 157, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(clearFormButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(quitButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(printToFileButton))
                .addGap(58, 58, 58))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(233, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addGap(212, 212, 212))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(168, Short.MAX_VALUE)
                .addComponent(enterDataButton, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(139, 139, 139))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(city, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(clearFormButton))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(state, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(quitButton))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(population, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(printToFileButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addComponent(enterDataButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    private void clearFormButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // When Clear Button is pressed, clear all text fields
        city.setText("");
        state.setText("");
        population.setText("");
    }                                        

    private void quitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // When EXIT Button is pressed, exit application
        System.exit(0);
    }                                        

    private void printToFileButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // When PRINT TO FILE Button is pressed, print all entered data to file population.txt
        FileOutputStream out; // declare a file output object
        PrintStream p; // declare a print stream object

        try
        {
            // Create a new file output stream
            // connected to "population.txt"
            out = new FileOutputStream("population.txt");

            // Connect print stream to the output stream
            p = new PrintStream( out );

            p.println ("Population by City and State");
            p.println("----------------------------");
            for (CityRecord rec : cityRecordList) 
            {
                p.println(rec);
            }

            p.close();
        }
        catch (Exception e)
        {
            System.err.println ("Error writing to file");
        }
    }

    private void enterDataButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        
        String cityName;
        String stateName;
        int populationNum;
        cityName = city.getText();
        stateName = state.getText();
        populationNum = Integer.parseInt(population.getText());

        // create a CityRecord object
        CityRecord cityRecord = new CityRecord(cityName, stateName, populationNum);

        // Add elements to array list
        cityRecordList.add(cityRecord);

        city.setText("");
        state.setText("");
        population.setText("");
    }                                        

    // Create an array list that can grow as data is input by user
    ArrayList<CityRecord> cityRecordList = new ArrayList<CityRecord>();
   
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PopulationForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JTextField city;
    private javax.swing.JButton clearFormButton;
    private javax.swing.JButton enterDataButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField population;
    private javax.swing.JButton printToFileButton;
    private javax.swing.JButton quitButton;
    private javax.swing.JTextField state;
    // End of variables declaration
}
 

Related Threads for: Java GUI output to text file

Replies
31
Views
10K
Replies
1
Views
1K
Replies
2
Views
2K
Replies
1
Views
1K
Replies
6
Views
2K
Replies
9
Views
9K
  • Last Post
Replies
3
Views
2K
Top