title: Java IOStream Arrays JFrame
body: <p>I have to create a CD inventory program for my first Java class.  The book is poorly written and extremely verbose.  I have created 4 frames to handle each requirement of the assignment.  But the book doesn't explain how to write arrays to a .dat file.  If I could get an idea of how to add data to an array from my TextFields then write to a .dat file I could stumble through the rest.  Here is what I have so far.  How do I take my JTextFields from my Add CD listener and write to a .dat file so I can view it later.</p>
  
  <pre><code>import java.util.*;
  import java.text.DecimalFormat;
  import java.text.NumberFormat;
  import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  import java.io.*;
  import java.lang.SecurityException;
  import java.util.Formatter;
  import java.util.FormatterClosedException;
  import java.util.NoSuchElementException;
  import java.util.Scanner;
  import javax.swing.border.Border;
  import javax.swing.border.LineBorder;
  
  
  
  class CDinventoryItem extends JFrame implements Comparable &lt;CDinventoryItem&gt; {
  
      private String sPtitle;
      private String genreCD;
      private int iPitemNumber;
      private int iPnumberofUnits;
      private double dPunitPrice;
      private double dEvalue;
      private JFrame frame2= new JFrame();
      private JFrame frame3= new JFrame();
      private JFrame frame4= new JFrame();
      private JPanel panel1 = new JPanel();
      private JPanel panel2 = new JPanel();
      private JPanel panel3 = new JPanel();
      private JPanel panel4 = new JPanel();
      private JPanel panel5 = new JPanel();
      private JPanel panel6 = new JPanel();
      private JLabel[] label = new JLabel[20];
      private JTextField titleField;
      private JTextField itemNField;
      private JTextField numofunitsField;
      private JTextField priceField;
      private JButton next;
      private JButton prev;
      private JButton addCD = new JButton ("Add CD");
      private JButton save = new JButton ("Save");
      private JButton delete;
      private JButton modify;
      private JButton search = new JButton ("Search for CD");
      private JButton mainmenu;
      private JButton displayCD = new JButton ("Display Inventory");
      private CDinventoryItem [] inven;
      private DataOutputStream outFile;
      private DataInputStream inputFile;
  
  
  
      public CDinventoryItem (String title, int itemNumber, int numberofUnits, 
      double unitPrice, String genre){
  
              sPtitle = title;
              iPitemNumber = itemNumber;
              iPnumberofUnits = numberofUnits;
              dPunitPrice = unitPrice;
              genreCD = genre;
  
  
          for(int i = 0; i &lt; label.length; i++) {
          label[i] = new JLabel();
          }
  
          setResizable(false);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
          panel1.add(label[0]);
          Icon bug = new ImageIcon( getClass().getResource( "mylogo.JPG" ) );
          label[0].setIcon( bug );
          label[0].setPreferredSize(new Dimension(400, 150));
  
          panel1.add(label[1]);
          label[1].setText("Press button to choose option:");
          label[1].setPreferredSize(new Dimension(400, 50));
  
  
          panel1.add(panel2);
          panel2.setPreferredSize(new Dimension(400, 50));
          ButtonListerner inputNewCD = new ButtonListerner();
          panel2.add(addCD);
          addCD.addActionListener(inputNewCD);
          ButtonListerner searchCD = new ButtonListerner();
          panel2.add(search);
          search.addActionListener(searchCD);
  
          ButtonListerner display = new ButtonListerner();
          panel2.add(displayCD);
          displayCD.addActionListener(display);
  
          setContentPane(panel1);
  
      }
  
  
      private class ButtonListerner implements ActionListener
      {
  
          public void actionPerformed ( ActionEvent event) {
  
  
          if (event.getActionCommand().equals("Add CD"))
          {
              frame2.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
              frame2.setLocation(525,100);
              frame2.setSize(425, 425);
              frame2.setVisible( true );
  
  
              frame2.add(panel3);
  
              panel3.add(label[0]);
  
              panel3.add(label[2]);
              label[2].setText("Enter title of CD:");
              label[2].setPreferredSize(new Dimension(100, 25));
  
              JTextField titleText = new JTextField(20);
              panel3.add (titleText);
              sPtitle.equals(titleText);
              label[2].setPreferredSize(new Dimension(175, 25));
  
              panel3.add(label[3]);
              label[3].setText("Enter number of CDs:");
  
              JTextField numCDText = new JTextField(5);
              panel3.add(numCDText);
              label[3].setPreferredSize(new Dimension(275, 25));
  
              panel3.add(label[4]);
              label[4].setText("Enter price of CD:");
              JTextField priceCDText = new JTextField(6);
              panel3.add(priceCDText);
  
              label[4].setPreferredSize(new Dimension(274, 25));
  
              panel3.add(label[5]);
              label[5].setText("Pick genre:");
              String stringBox[] = {"Drama","Action","Comedy"};
              JComboBox comboBox = new JComboBox(stringBox);
              comboBox.setEditable(false);
              panel3.add(comboBox);
  
              panel3.add(save);
  
  }
  
          if (event.getActionCommand().equals("Search for CD")){
  
  
              frame3.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
              frame3.setLocation(100,525);
              frame3.setSize(425, 425);
              frame3.setVisible( true );
  
              frame3.add(panel4);
  
              panel4.add(label[0]);
  
              panel4.add(label[6]);
              label[6].setText("Enter name of CD you want to search for:");
              JTextField searchName = new JTextField(20);
              panel4.add(searchName);
  
              panel4.add(label[7]);
              label[7].setText("Or search by genre:");
              JCheckBox checkB1 = new JCheckBox("Drama");
              JCheckBox checkB2 = new JCheckBox("Action");
              JCheckBox checkB3 = new JCheckBox("Comedy");
              panel4.add(checkB1);
              panel4.add(checkB2);
              panel4.add(checkB3);
  
  
          }
  
          if (event.getActionCommand().equals("Display Inventory")){
  
              frame4.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
              frame4.setLocation(525,525);
              frame4.setSize(425, 425);
              frame4.setVisible( true );
  
              frame4.add(panel5);
  
              panel5.add(label[0]);
  
              panel5.add(label[8]);
              label[8].setText("List of CDs:");
  
          }
  
          if (event.getActionCommand().equals("Save")){
  
              String dataFile = "inventory.dat";
  
              try{
              outFile = new DataOutputStream(new BufferedOutputStream(
              new FileOutputStream(dataFile)));
              }
              catch(FileNotFoundException fileNotFoundException ){
  
              }
  
          }
  }}
  
      public int compareTo(CDinventoryItem otherItem) {
      return this.sPtitle.compareTo(otherItem.getTitle());
  }
  
  @Override
      public String getTitle() {
          return sPtitle;
  }
  
  }
  
  
  public class CDinventoryprogram {
  
  
  public static void main(String[] args) {
  
      JOptionPane.showMessageDialog( null, "Welcome to my CD inventory program!!"
  ,  "Inventory", JOptionPane.INFORMATION_MESSAGE);
  
      CDinventoryItem initem = new CDinventoryItem ("", 0, 0, 0.0,"" );
      initem.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
      initem.setLocation(100,100);
      initem.setSize(425, 425);
      initem.setVisible( true );
  
  }
  </code></pre>