saving as a .dat file
I know I need to use jfilechooser and a write in and out but I can not
figure out how to add a way to save this to a .dat file
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JFrame.*;
class Testing
{
java.util.List<DVD> dvds = new java.util.ArrayList<DVD>();
JTextField tfTitle = new JTextField(5);
JTextField tfGenre = new JTextField();
JTextField tfProductNumber = new JTextField();
JTextField tfPrice = new JTextField();
JTextField tfQuantity = new JTextField();
JTextField tfvalue = new JTextField();
JTextField tfrestock = new JTextField();
DefaultListModel dlm = new DefaultListModel();
JList list = new JList(dlm);
public void buildGUI()
{
JButton btnAdd = new JButton("Add");
JButton btnPrevious = new JButton ("Previous");
JButton btnNext = new JButton ("Next");
JButton btnFirst = new JButton ("First");
JButton btnLast = new JButton ("Last");
JPanel p1 = new JPanel(new BorderLayout());
JPanel p = new JPanel(new GridLayout(8,4));
p.add(new JLabel("DVD Title: "));
p.add(tfTitle);
p.add(new JLabel("Genre: "));
p.add(tfGenre);
p.add(new JLabel("Product Number: "));
p.add(tfProductNumber);
p.add(new JLabel("Price per Unit: "));
p.add(tfPrice);
p.add(new JLabel("Quantity on Hand: "));
p.add(tfQuantity);
p.add(new JLabel("Value: "));
p.add(tfvalue);
p.add(new JLabel("Restock-fee: "));
p.add(tfrestock);
p1.add(p,BorderLayout.NORTH);
JPanel p2 = new JPanel();
p2.add(btnAdd);
p2.add(btnPrevious);
p2.add(btnNext);
p2.add(btnFirst);
p2.add(btnLast);
p1.add(p2,BorderLayout.CENTER);
JFrame f = new JFrame();
f.setIconImage(new ImageIcon("bullet.gif").getImage());
f.getContentPane().add(p1,BorderLayout.NORTH);
JScrollPane sp = new JScrollPane(list);
f.getContentPane().add(sp,BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
f.setIconImage(new ImageIcon("Images/bullet.gif").getImage());
btnAdd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
dvds.add(new DVD(tfTitle.getText(),tfGenre.getText(),
tfProductNumber.getText(),
Double.parseDouble(tfPrice.getText()),
Double.parseDouble(tfvalue.getText()),
Double.parseDouble(tfrestock.getText()),
Integer.parseInt(tfQuantity.getText())));
setList();
clearTextFields();
}
});
btnPrevious.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
DVD dvd = (DVD)dvds.get(list.getSelectedIndex());
int index = (list.getSelectedIndex()-1) % dvds.size();
if(index < 0) index = dvds.size()-1;
list.setSelectedIndex(index);
}
});
btnNext.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
DVD dvd = (DVD)dvds.get(list.getSelectedIndex());
int index = (list.getSelectedIndex()+1) % dvds.size();
if(index < 0) index = dvds.size()+1;
list.setSelectedIndex(index);
}
});
btnFirst.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
DVD dvd = (DVD)dvds.get(0);
list.setSelectedIndex(0);
}
});
btnLast.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
DVD dvd = (DVD)dvds.get(5);
list.setSelectedIndex(5);
}
});
list.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent lse){
if(lse.getValueIsAdjusting() == false)
{
DVD dvd = (DVD)dvds.get(list.getSelectedIndex());
tfTitle.setText(dvd.title);
tfGenre.setText(dvd.genre);
tfProductNumber.setText(dvd.productNumber);
tfPrice.setText(""+dvd.price);
tfQuantity.setText(""+dvd.quantity);
tfvalue.setText(""+dvd.value);
tfrestock.setText(""+dvd.restock);
}
}
});
}
public void setList()
{
dlm.clear();
for(int x = 0, y = dvds.size(); x < y; x++)
{
dlm.addElement((DVD)dvds.get(x));
}
}
public void clearTextFields()
{
tfTitle.setText("");
tfGenre.setText("");
tfProductNumber.setText("");
tfPrice.setText("");
tfQuantity.setText("");
tfvalue.setText("");
tfrestock.setText("");
tfTitle.requestFocusInWindow();
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable(){
public void run(){
new Testing().buildGUI();
}
});
}
}
class DVD
{
String title;
String genre;
String productNumber;
double price;
double value;
double restock;
int quantity;
public DVD(String t, String g, String pn, double p, double v, double
r, int q)
{
title = t; genre = g; productNumber = pn; price = p; value = v;
restock = r; quantity = q;
value = p * q;
restock = v * 1.05;
}
public String toString(){return title;}
}