Need some help with this fantasy football program, struggling to figure out how to implement my writetofile
method into the add button and then to have it save on close when I exit the program.
I've been told by a friend to use a WindowListener
but have no idea how to use this.
import java.awt.GridLayout;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.SwingUtilities;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.*;
import java.util.*;
public class JListFBExample extends JFrame
{
private JList<Footballer> rhsList;
private DefaultListModel<Footballer> rhsListModel;
private JButton btnIn;
private JButton btnOut;
private JButton btnAdd;
private JButton btnDelete;
private JButton acceptPlayer;
private JTextField playerFName;
private JTextField playerLName;
private JComboBox comboBox;
private JList<Footballer> fbList;
private ArrayList<Footballer> fbArrayList = new ArrayList<Footballer();
private DefaultListModel<Footballer> fbListModel;
public JListFBExample() throws IOException, ClassNotFoundException
{
readRecordsFromFile();
//create the model and add elements
fbListModel = new DefaultListModel<>();
for(Footballer f : fbArrayList)
fbListModel.addElement(f);
fbList = new JList<>(fbListModel);
//create the model and add elements
rhsListModel = new DefaultListModel<>();
rhsList = new JList<>(rhsListModel);
setLayout(new GridLayout(1, 3));
add(new JScrollPane(fbList)); //add the list to scrollpane and to the frame
add(createButtonPanel());
add(new JScrollPane(rhsList));
}
public JPanel createButtonPanel()
{
JPanel panel = new JPanel();
ActionListener listener = new ButtonListener();
ActionListener delListener = new DeleteListener();
btnIn = new JButton(">>");
btnOut = new JButton("<<");
btnAdd = new JButton("Add");
btnDelete = new JButton("Delete");
btnIn.addActionListener(listener);
btnOut.addActionListener(listener);
btnDelete.addActionListener(delListener);
btnAdd.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
int i = JOptionPane.showConfirmDialog(null, addPlayerPanel(), "Add player", JOptionPane.OK_CANCEL_OPTION);
if(i== JOptionPane.OK_OPTION)
{
Footballer f = new Footballer(playerFName.getText(), playerLName.getText(), (String)comboBox.getSelectedItem());
fbListModel.addElement(f);
}
}
});
panel.add(btnIn);
panel.add(btnOut);
panel.add(btnAdd);
panel.add(btnDelete);
return panel;
}
public JPanel addPlayerPanel()
{
JPanel playerPanel = new JPanel(new GridLayout(3,1));
playerFName = new JTextField(15);
playerLName = new JTextField(15);
String[] options = { "Goalkeeper", "Defender", "Midfielder", "Forward"};
comboBox = new JComboBox(options);
playerPanel.add(playerFName);
playerPanel.add(playerLName);
playerPanel.add(comboBox);
return playerPanel;
}
public class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(btnIn))
{
int[] fromIndex = fbList.getSelectedIndices();
Object[] from = fbList.getSelectedValues();
for(Object s : from) //for each object in from -- in the selected list
//add to rhslist
rhsListModel.addElement((Footballer)s);
for(Object s : from)
fbListModel.removeElement((Footballer)s);
}
else if(e.getSource().equals(btnOut))
{
int[] fromIndex = rhsList.getSelectedIndices();
Object[] from = rhsList.getSelectedValues();
for(Object s : from)
fbListModel.addElement((Footballer)s);
for(Object s : from)
rhsListModel.removeElement((Footballer)s);
}
}
}
public class DeleteListener implements ActionListener
{
public void actionPerformed(ActionEvent d)
{
if(d.getSource().equals(btnDelete))
{
int[] fromIndex = fbList.getSelectedIndices();
Object[] from = fbList.getSelectedValues();
for(Object s : from)
fbListModel.removeElement((Footballer)s);
}
}
}
// writeRecordsToFile()
public void writeRecordsToFile() throws IOException,FileNotFoundException
{
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("players.bin"));
os.writeObject(fbArrayList);
os.close();
}
//readRecordsFromFile()
public void readRecordsFromFile() throws IOException, ClassNotFoundException
{
ObjectInputStream is = new ObjectInputStream (new FileInputStream("players.bin"));
fbArrayList = (ArrayList<Footballer>)is.readObject(); //note use of cast
is.close();
}
}