Hi there,
I am new to Java and have a small problem that I hope somebody can help me
with. I have a class videoRecord that is used to hold data about videos in
a rental libraray. It reads this data from a text file (video.txt)
I have another class VideoStore that creates a form with some buttons for
New/Add.First/Next.
When I fire up the app I can add a new record. If I then clcik on First it
shows this record. If I then add another record and click on First it only
shows this one. The original record has been overwritten by the second one.
I have pasted the code from both my classes into this post in the hopes
that somebody can point me in the right direction.
Thanks in advance.
*************************************************************
//videoRecord CLASS
/*
* Video.java
* Created on 25 April 2004, 16:04
*/
package VideoStore.VideoRecord;
import java.io.*;
public class videoRecord implements Serializable{
private String classification;
private String title;
private float duration;
private float hireCost;
/** Creates a new instance of VideoRecord */
public videoRecord() {
this("","",0,0);
}
public videoRecord(String c, String t, float d, float h) {
setClassification(c);
setTitle(t);
setDuration(d);
setHireCost(h);
}
public void setClassification(String c){
classification = c;
}
public String getClassification(){
return classification;
}
public void setTitle(String t){
title = t;
}
public String getTitle(){
return title;
}
public void setDuration(float d){
duration = d;
}
public float getDuration(){
return duration;
}
public void setHireCost(float h){
hireCost = h;
}
public float getHireCost(){
return hireCost;
}
}
*************************************************************
*************************************************************
//VideoStore CLASS
/*
* VideoStore.java
*
* Created on 16 June 2004, 13:51
*/
package VideoStore.VideoRecord;
import java.io.*;
import javax.swing.*;
/**
*
* @author allenjones
*/
public class VideoStore extends javax.swing.JFrame {
ObjectInputStream input;
File file = new File(new
File(getClass().getResource("data").getFile()),"video.txt");
/** Creates new form VideoStore */
public VideoStore() {
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.
*/
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
txtClassification = new javax.swing.JTextField();
txtTitle = new javax.swing.JTextField();
txtDuration = new javax.swing.JTextField();
txtHireCost = new javax.swing.JTextField();
cmdNew = new javax.swing.JButton();
cmdAdd = new javax.swing.JButton();
cmdFirst = new javax.swing.JButton();
cmdNext = new javax.swing.JButton();
getContentPane().setLayout(new
org.netbeans.lib.awtextra.AbsoluteLayout());
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jLabel1.setText("Class");
getContentPane().add(jLabel1, new
org.netbeans.lib.awtextra.AbsoluteConstraints(30, 20, 50, 20));
jLabel2.setText("Title");
getContentPane().add(jLabel2, new
org.netbeans.lib.awtextra.AbsoluteConstraints(30, 50, 50, 20));
jLabel3.setText("Duration");
getContentPane().add(jLabel3, new
org.netbeans.lib.awtextra.AbsoluteConstraints(30, 80, 50, 20));
jLabel4.setText("Hire Cost");
getContentPane().add(jLabel4, new
org.netbeans.lib.awtextra.AbsoluteConstraints(30, 110, 50, 20));
getContentPane().add(txtClassification, new
org.netbeans.lib.awtextra.AbsoluteConstraints(100, 20, 90, -1));
getContentPane().add(txtTitle, new
org.netbeans.lib.awtextra.AbsoluteConstraints(100, 50, 90, -1));
getContentPane().add(txtDuration, new
org.netbeans.lib.awtextra.AbsoluteConstraints(100, 80, 90, -1));
getContentPane().add(txtHireCost, new
org.netbeans.lib.awtextra.AbsoluteConstraints(100, 110, 90, -1));
cmdNew.setText("New");
cmdNew.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdNewActionPerformed(evt);
}
});
getContentPane().add(cmdNew, new
org.netbeans.lib.awtextra.AbsoluteConstraints(270, 20, -1, -1));
cmdAdd.setText("Add");
cmdAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdAddActionPerformed(evt);
}
});
getContentPane().add(cmdAdd, new
org.netbeans.lib.awtextra.AbsoluteConstraints(270, 50, -1, -1));
cmdFirst.setText("First");
cmdFirst.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdFirstActionPerformed(evt);
}
});
getContentPane().add(cmdFirst, new
org.netbeans.lib.awtextra.AbsoluteConstraints(30, 160, -1, -1));
cmdNext.setText("Next");
cmdNext.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdNextActionPerformed(evt);
}
});
getContentPane().add(cmdNext, new
org.netbeans.lib.awtextra.AbsoluteConstraints(100, 160, -1, -1));
java.awt.Dimension screenSize =
java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-348)/2, (screenSize.height-228)/2, 348,
228);
}
private void cmdNextActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
videoRecord record;
try {
record = (videoRecord)input.readObject();
txtClassification.setText(record.getClassification());
txtTitle.setText(record.getTitle());
txtDuration.setText(Float.toString(record.getDuration()));
txtHireCost.setText(Float.toString(record.getHireCost()));
}
catch (EOFException e)
{
clearTexts();
cmdNext.setEnabled(false);
}
catch (Exception a)
{
JOptionPane.showMessageDialog(this, "Error here", "Error",
JOptionPane.ERROR_MESSAGE);
cmdNext.setEnabled(false);
}
}
private void cmdFirstActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
videoRecord record;
try {
input = new ObjectInputStream(new FileInputStream(file));
record = (videoRecord)input.readObject();
txtClassification.setText(record.getClassification());
txtTitle.setText(record.getTitle());
txtDuration.setText(Float.toString(record.getDuration()));
txtHireCost.setText(Float.toString(record.getHireCost()));
cmdNext.setEnabled(true);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(this, "Can't find record",
"Error", JOptionPane.ERROR_MESSAGE);
cmdNext.setEnabled(false);
}
catch (ClassNotFoundException e)
{
JOptionPane.showMessageDialog(this, "Can't find record",
"Error", JOptionPane.ERROR_MESSAGE);
cmdNext.setEnabled(false);
}
}
private void cmdAddActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
videoRecord record;
try {
record = new videoRecord(
txtClassification.getText(),
txtTitle.getText(),
Float.parseFloat(txtDuration.getText()),
Float.parseFloat(txtHireCost.getText()));
FileOutputStream ostr = new FileOutputStream(file);
ObjectOutputStream oostr = new ObjectOutputStream(ostr);
oostr.writeObject(record);
//oostr.close(); // may be necessary to complete the write
clearTexts();
}
catch (IOException e)
{
JOptionPane.showMessageDialog(this, "Something wrong!",
"Error", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
private void cmdNewActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
clearTexts();
}
catch (Exception e){
JOptionPane.showMessageDialog(this, "Something wrong!", Error",
JOptionPane.ERROR_MESSAGE);
//cmdAdd.setEnabled(false);
e.printStackTrace();
}
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new VideoStore().show();
}
public void clearTexts()
{
txtClassification.setText("");
txtTitle.setText("");
txtDuration.setText("");
txtHireCost.setText("");
}
// Variables declaration - do not modify
private javax.swing.JButton cmdAdd;
private javax.swing.JButton cmdFirst;
private javax.swing.JButton cmdNew;
private javax.swing.JButton cmdNext;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JTextField txtClassification;
private javax.swing.JTextField txtDuration;
private javax.swing.JTextField txtHireCost;
private javax.swing.JTextField txtTitle;
// End of variables declaration
}
*************************************************************
--
ats@home
When an old lady got hit by a truck
I saw a wicked gleam in your eye
Handy utils here:
http://www.allenjones.co.uk/utils.htm