I having a small problem with the output on the Your CD's side, they are only viewable if I double click inside the pane, then they show up, almost like the pane is on top. How can I fix, I have tried moving things around and changing the TextArea to a TextField, but to no avail.

/**
 * Write a description of class CD here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CD
{
    //The purpose of this class is to create the object of the CD with a title, category,
    //rating, artist
    private String artist;
    private String title;
    private String category;
    private int rating;
    public CD()
    {
        //constructor
        artist = "";
        title = "";
        category = "";
        rating = 0;
    }
    public CD(String _title, String _category, int _rating, String _artist)
    {
        //for ease, allow a constructor that asks for 
        //all information at the beginning
        
        title = _title;
        category = _category;
        rating = _rating;
        artist = _artist;
    }
    //methods to retrieve information
    public String getArtist()
    {
        return artist;
    }
    public String getTitle()
    {
        return title;
    }
    public String getCategory()
    {
        return category;
    }
    public int getRating()
    {
        return rating;
    }
}

 
 
/**
 * Write a description of class CDTester here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import java.io.*;
// The purpose of this class is to create the input window interface.
// Needs to extends JFrame so we can make the parameters.
public class CDFrame extends JFrame 
{
    //make instance of Collection class.
    Collection C = new Collection();
    
    //throws IOException to catch illegal input/output
    public CDFrame() throws IOException
    {
       setSize(FRAME_WIDTH,FRAME_HEIGHT);
       //creates panels that perform methods.
       JPanel addCDPanel = addCD();
       JPanel displayCDPanel = displayCD();
       //adds border
       displayCDPanel.setBorder(new TitledBorder(new EtchedBorder(), "Your CDs"));
       add(displayCDPanel, BorderLayout.CENTER);
       //adds border
       addCDPanel.setBorder(new TitledBorder(new EtchedBorder(), "Add CDs"));
       add(addCDPanel, BorderLayout.WEST);
       displayCollection();
       //C.readData();
    }
    public JPanel displayCD()
    {
        
      //makes the CD display window and sets the dimensions
        JPanel panel2 = new JPanel();
        panel2.setPreferredSize(new Dimension(500,500));
        //panel.setLocation(100,100);
        panel2.setBackground(Color.WHITE);
        box5.setLineWrap(true);
        box5.setWrapStyleWord(true);
        box5.setEditable(false);
        panel2.add(box5);
        return panel2;
    }
    
    public JPanel addCD() throws IOException
   // throws IOException to allow illegal input/output
    {
        //needed to access data for graphical interface
        class addNewCD implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            try
            {
            // This adds the CD to the viewing window
            String artistTemp = box.getText();
            String titleTemp = box2.getText();
            String catTemp = box3.getText();
            int ratingTemp = Integer.parseInt(box4.getText());
            C.addCD(titleTemp,catTemp,ratingTemp,artistTemp);
            //readDataOntoScreen();
            JPanel displayCDPanel = displayCD();
            add(displayCDPanel, BorderLayout.CENTER);
            displayCollection();
            clearInput();
            }
            // try catch to bypass illegal values
            catch (NumberFormatException e)
            {
                System.out.println(e);    
                label5.setText("One or more fields are invalid.");
                label6.setText("Check your input.");
                System.out.println("NOES!");
            }
            
           // C.writeData();
            
            
        }
    }
        ActionListener listener = new addNewCD();
        button.addActionListener(listener);
   // creates interface
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(260,50));
        //panel.setLocation(100,100);
        panel.setBackground(Color.WHITE);
    //creates interface by adding labels and text boxes    
        panel.add(label);
        panel.add(box);
        panel.add(label2);
        panel.add(box2);
        panel.add(label3);
        panel.add(box3);
        panel.add(label4);
        panel.add(box4);
        panel.add(button);
        panel.add(label5);
        panel.add(label6);
        
        return panel;
    }
   // method to display cdcollection 
    public void displayCollection()
    {
        String tempString = "";
        for (int x = 0; x < C.getCount(); x++)
        {
            CD mike = C.getData(x);
           // writes data in order that user is instructed to input it
            tempString += "Artist: " + mike.getArtist() + "\nTitle: " + mike.getTitle() + "\nCategory: " + mike.getCategory() + "\nRating: " + mike.getRating() + "\n\n"; 
        }
        System.out.println(tempString);
        box5.append(tempString);
       
    }
  // this is created to display the current CD collection on the viewing window.  
    public void readDataOntoScreen()
    {
        String str;
       // BufferedReader br;
        try
        {
            // our file will be read from C:\\file.txt
            File f = new File("C:\\Documents and Settings\\cs306.CS-00\\Desktop\\File.txt");
            br = new BufferedReader(new FileReader(f));
            while((str = br.readLine()) != null)
            {
                box5.append(str);
            }
        }
        // add catch to state wheather or not file exists
        catch (FileNotFoundException e)
        {
            System.out.println(e);
        }
        
        catch (IOException ioe)
        {
            System.out.println(ioe);
        }
        finally
        {
            try
            {
                //required. you must close a FileReader
                br.close();
            }
            catch (Exception ignored)
            {
            }
        }
    }
      // this resets your data fields every time      
    public void clearInput()
    {
            box.setText(null);
            box2.setText(null);
            box3.setText(null);
            box4.setText(null);
            label5.setText(null);
            label6.setText(null);
    }
    
       //this creates all of our labels and text fields. These are where the user interacts
        JLabel label = new JLabel ("Enter Artist name:");
        final JTextField box = new JTextField(null,20);       
        JLabel label2 = new JLabel ("Enter CD Title:");
        final JTextField box2 = new JTextField(null,20);
        JLabel label3 = new JLabel ("Enter Category:");
        final JTextField box3 = new JTextField(null,20);
        JLabel label4 = new JLabel ("Enter your rating for CD between 1 and 5:");
        JLabel label5 = new JLabel();
        JLabel label6 = new JLabel();
        final JTextField box4 = new JTextField(null,20);
        // button to implement methods
        JButton button = new JButton("Add CD");
        final JTextArea box5 = new JTextArea(5,50);
       
        // intilize frame
        public BufferedReader br;
        final int FRAME_WIDTH = 500;
        final int FRAME_HEIGHT = 500;
}
        
     
 
import java.util.ArrayList;
import java.io.*;
import java.util.Scanner;
public class Collection
{
   // public does not need to see this
    private File fileName;
    private ArrayList<CD> album = new ArrayList<CD>();
    private Scanner input;
    
    public Collection()
    {
        // initiize the file we are writing to
        fileName = new File("C:\\Documents and Settings\\cs306.CS-00\\Desktop\\File.txt");
        readData();
    }
    public Collection(String filename)
    {
        //so we can use any file we want to
        fileName = new File(filename);
        readData();
    }
    // addCD method that will be used in other classes
    public void addCD(String _title, String _category, int _rating, String _artist)
    {
        album.add(new CD(_title, _category, _rating, _artist));
        // catch illegal input
        try
        {
            writeData();
        }
        catch (IOException s)
        {
            System.out.println(s.toString());
        }
    }
    // This reads data from our file.txt
    private void readData()
    {
        try
        {
            album.clear();
            input = new Scanner(fileName);
            while (input.hasNext())
            {
                String s = input.nextLine();
                String[] cd = s.split("ÿ");
                album.add(new CD(cd[0], cd[1], Integer.parseInt(cd[2]), cd[3]));
            }
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
        }
        
        finally
        {
            input.close();
        }
    }
   //we did not use this 
    public int getCount()
    {
       return album.size();
    }
    // gets data from the file
    public CD getData(int index)
    {
        return album.get(index);
    }
    
    // This is our method to write to file.txt. We are not returning anything right here
    private void writeData() throws IOException
    {
        PrintWriter output = new PrintWriter(fileName);
        for(CD cd: album)
        {
           // write with delimeters
            output.write(cd.getTitle() + "ÿ" + cd.getCategory() + "ÿ" + cd.getRating() + "ÿ" + cd.getArtist() + "\n");
            //output.print(cd);
        }
        //close PrintWriter
        output.close();
    }
}

 
import javax.swing.JFrame;
import java.io.*;
// this program tests the CDFrame
public class CDFrameViewer
{
   // this is our main
     public static void main(String[]args) throws IOException
    {
        // make instance of CDFrame
        JFrame frame = new CDFrame();
        //required for interface
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Thanks in advance!!!

See bold red comments in the code:

I having a small problem with the output on the Your CD's side, they are only viewable if I double click inside the pane, then they show up, almost like the pane is on top. How can I fix, I have tried moving things around and changing the TextArea to a TextField, but to no avail.

/**
 * Write a description of class CD here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CD
{
    //The purpose of this class is to create the object of the CD with a title, category,
    //rating, artist
    private String artist;
    private String title;
    private String category;
    private int rating;
    public CD()
    {
        //constructor
        artist = "";
        title = "";
        category = "";
        rating = 0;
    }
    public CD(String _title, String _category, int _rating, String _artist)
    {
        //for ease, allow a constructor that asks for 
        //all information at the beginning
        
        title = _title;
        category = _category;
        rating = _rating;
        artist = _artist;
    }
    //methods to retrieve information
    public String getArtist()
    {
        return artist;
    }
    public String getTitle()
    {
        return title;
    }
    public String getCategory()
    {
        return category;
    }
    public int getRating()
    {
        return rating;
    }
}

 
 
/**
 * Write a description of class CDTester here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import java.io.*;
// The purpose of this class is to create the input window interface.
// Needs to extends JFrame so we can make the parameters.
public class CDFrame extends JFrame 
{
    //make instance of Collection class.
    Collection C = new Collection();
    
    //throws IOException to catch illegal input/output
    public CDFrame() throws IOException
    {
       setSize(FRAME_WIDTH,FRAME_HEIGHT);
       //creates panels that perform methods.
       JPanel addCDPanel = addCD();
       JPanel displayCDPanel = displayCD();
       //adds border
       displayCDPanel.setBorder(new TitledBorder(new EtchedBorder(), "Your CDs"));
       add(displayCDPanel, BorderLayout.CENTER);
       //adds border
       addCDPanel.setBorder(new TitledBorder(new EtchedBorder(), "Add CDs"));
       add(addCDPanel, BorderLayout.WEST);
       displayCollection();
       //C.readData();
    }
    public JPanel displayCD()
    {
        
      //makes the CD display window and sets the dimensions
        JPanel panel2 = new JPanel();
        [B]panel2.setPreferredSize(new Dimension(500,500));  // same size as your whole frame - may want to reduce it[/B]
        //panel.setLocation(100,100);
        panel2.setBackground(Color.WHITE);
        box5.setLineWrap(true);
        box5.setWrapStyleWord(true);
        box5.setEditable(false);
        panel2.add(box5);
        return panel2;
    }
    
    public JPanel addCD() throws IOException
   // throws IOException to allow illegal input/output
    {
        //needed to access data for graphical interface
        class addNewCD implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            try
            {
            // This adds the CD to the viewing window
            String artistTemp = box.getText();
            String titleTemp = box2.getText();
            String catTemp = box3.getText();
            int ratingTemp = Integer.parseInt(box4.getText());
            C.addCD(titleTemp,catTemp,ratingTemp,artistTemp);
            //readDataOntoScreen();
            [B]//JPanel displayCDPanel = displayCD();  // you don't need to create new panels
            //add(displayCDPanel, BorderLayout.CENTER);[/B]
            displayCollection();
            clearInput();
            }
            // try catch to bypass illegal values
            catch (NumberFormatException e)
            {
                System.out.println(e);    
                label5.setText("One or more fields are invalid.");
                label6.setText("Check your input.");
                System.out.println("NOES!");
            }
            
           // C.writeData();
            
            
        }
    }
        ActionListener listener = new addNewCD();
        button.addActionListener(listener);
   // creates interface
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(260,50));
        //panel.setLocation(100,100);
        panel.setBackground(Color.WHITE);
    //creates interface by adding labels and text boxes    
        panel.add(label);
        panel.add(box);
        panel.add(label2);
        panel.add(box2);
        panel.add(label3);
        panel.add(box3);
        panel.add(label4);
        panel.add(box4);
        panel.add(button);
        panel.add(label5);
        panel.add(label6);
        
        return panel;
    }
   // method to display cdcollection 
    public void displayCollection()
    {
        String tempString = "";
        for (int x = 0; x < C.getCount(); x++)
        {
            CD mike = C.getData(x);
           // writes data in order that user is instructed to input it
            tempString += "Artist: " + mike.getArtist() + "\nTitle: " + mike.getTitle() + "\nCategory: " + mike.getCategory() + "\nRating: " + mike.getRating() + "\n\n"; 
        }
        System.out.println(tempString);
        [B]box5.setText(tempString); // changed from .append()[/B]
       
    }
  // this is created to display the current CD collection on the viewing window.  
    public void readDataOntoScreen()
    {
        String str;
       // BufferedReader br;
        try
        {
            // our file will be read from C:\\file.txt
            File f = new File("C:\\Documents and Settings\\cs306.CS-00\\Desktop\\File.txt");
            br = new BufferedReader(new FileReader(f));
            while((str = br.readLine()) != null)
            {
                box5.append(str);
            }
        }
        // add catch to state wheather or not file exists
        catch (FileNotFoundException e)
        {
            System.out.println(e);
        }
        
        catch (IOException ioe)
        {
            System.out.println(ioe);
        }
        finally
        {
            try
            {
                //required. you must close a FileReader
                br.close();
            }
            catch (Exception ignored)
            {
            }
        }
    }
      // this resets your data fields every time      
    public void clearInput()
    {
            box.setText(null);
            box2.setText(null);
            box3.setText(null);
            box4.setText(null);
            label5.setText(null);
            label6.setText(null);
    }
    
       //this creates all of our labels and text fields. These are where the user interacts
        JLabel label = new JLabel ("Enter Artist name:");
        final JTextField box = new JTextField(null,20);       
        JLabel label2 = new JLabel ("Enter CD Title:");
        final JTextField box2 = new JTextField(null,20);
        JLabel label3 = new JLabel ("Enter Category:");
        final JTextField box3 = new JTextField(null,20);
        JLabel label4 = new JLabel ("Enter your rating for CD between 1 and 5:");
        JLabel label5 = new JLabel();
        JLabel label6 = new JLabel();
        final JTextField box4 = new JTextField(null,20);
        // button to implement methods
        JButton button = new JButton("Add CD");
        final JTextArea box5 = new JTextArea[B]()[/B]; [B]// you don't really need to set the rows and columns here[/B]
       
        // intilize frame
        public BufferedReader br;
        final int FRAME_WIDTH = 500;
        final int FRAME_HEIGHT = 500;
}
        
     
 
import java.util.ArrayList;
import java.io.*;
import java.util.Scanner;
public class Collection
{
   // public does not need to see this
    private File fileName;
    private ArrayList<CD> album = new ArrayList<CD>();
    private Scanner input;
    
    public Collection()
    {
        // initiize the file we are writing to
        fileName = new File("C:\\Documents and Settings\\cs306.CS-00\\Desktop\\File.txt");
        readData();
    }
    public Collection(String filename)
    {
        //so we can use any file we want to
        fileName = new File(filename);
        readData();
    }
    // addCD method that will be used in other classes
    public void addCD(String _title, String _category, int _rating, String _artist)
    {
        album.add(new CD(_title, _category, _rating, _artist));
        // catch illegal input
        try
        {
            writeData();
        }
        catch (IOException s)
        {
            System.out.println(s.toString());
        }
    }
    // This reads data from our file.txt
    private void readData()
    {
        try
        {
            album.clear();
            input = new Scanner(fileName);
            while (input.hasNext())
            {
                String s = input.nextLine();
                String[] cd = s.split("ÿ");
                album.add(new CD(cd[0], cd[1], Integer.parseInt(cd[2]), cd[3]));
            }
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
        }
        
        finally
        {
            input.close();
        }
    }
   //we did not use this 
    public int getCount()
    {
       return album.size();
    }
    // gets data from the file
    public CD getData(int index)
    {
        return album.get(index);
    }
    
    // This is our method to write to file.txt. We are not returning anything right here
    private void writeData() throws IOException
    {
        PrintWriter output = new PrintWriter(fileName);
        for(CD cd: album)
        {
           // write with delimeters
            output.write(cd.getTitle() + "ÿ" + cd.getCategory() + "ÿ" + cd.getRating() + "ÿ" + cd.getArtist() + "\n");
            //output.print(cd);
        }
        //close PrintWriter
        output.close();
    }
}

 
import javax.swing.JFrame;
import java.io.*;
// this program tests the CDFrame
public class CDFrameViewer
{
   // this is our main
     public static void main(String[]args) throws IOException
    {
        // make instance of CDFrame
        JFrame frame = new CDFrame();
        //required for interface
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Thanks in advance!!!

Comments
Great debuging machine ;)

Thank you!! I got so wrapped in trying to fix other problems on it I didn't even see the size issue.

This article has been dead for over six months. Start a new discussion instead.