I am trying to create a program that uses a JFileChooser to save itself. When I try to save, I get an IO exception.
The message along with this IO exception says :
javax.swing.plaf.metal.MetalFileChooserUI. That is the entire message, so it didn't help much. I looked into the javax.swing.plaf.metal.MetalFileChooserUI class and saw that it was not serializable. The javax.swing.plaf.metal.MetalFileChooserUI class sets the look and feel for the JFileChooser that is used for saving files. I assumed this is what was causing the exception, so I made the JFileChooser transient, but the exception still occurs. If anyone could help, it would be greatly appreciated. Here is my code, with the parts that I think are making it buggy highlighted by a line of asterisks
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class FlashCardFrame extends JFrame implements Serializable {
private ArrayList<FlashCardData> cards;
private int index;
private JMenuBar menuBar;
private transient JMenu file;
private transient JMenuItem saveFile;
private transient JMenuItem openFile;
private transient JMenuItem run;
private JPanel content;
private JTextArea question;
private JTextArea answer;
private JButton previous;
private JButton next;
private JButton save;
/******************************************************************************
private transient JFileChooser saveFC;
private transient JFileChooser openFC;
******************************************************************************/
// Columns and Rows in question/answer boxes
private final int QACOLS = 40;
private final int QAROWS = 10;
public FlashCardFrame() {
// Set up the frame and content pane
super("Flash Card Creator");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
cards = new ArrayList<FlashCardData>();
content = new JPanel();
content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
content.setBackground(Color.white);
// index keeps track of what card is being viewed
index = 0;
// Set up the MenuBar
menuBar = new JMenuBar();
// Set up the file menu
file = new JMenu("File");
saveFile = new JMenuItem("Save");
saveFile.addActionListener(new SaveListener(this));
file.add(saveFile);
openFile = new JMenuItem("Open");
openFile.addActionListener(new OpenListener(this));
file.add(openFile);
run = new JMenuItem("Run");
run.addActionListener(new RunListener());
file.add(run);
menuBar.add(file);
setJMenuBar(menuBar);
// Set up the Question text area
question = new JTextArea(QAROWS, QACOLS);
question.setBorder(BorderFactory.createTitledBorder("Question"));
content.add(question);
// Add space between the question and answer boxes
/***** fillerSize will be used again later in
the program
*****/
Dimension fillerSize = new Dimension(0, 15);
content.add(Box.createRigidArea(fillerSize));
// Set up the Answer text area
answer = new JTextArea(QAROWS, QACOLS);
answer.setBorder(BorderFactory.createTitledBorder("Answer"));
content.add(answer);
// create a panel to hold buttons
JPanel buttons = new JPanel();
buttons.setLayout(new BoxLayout(buttons, BoxLayout.LINE_AXIS));
// create space that will go between buttons
Dimension buttonSpace = new Dimension(10, 0);
// the previous button displays the previous flash
// card, if there is a previous one
previous = new JButton("Previous Card");
previous.addActionListener(new PreviousListener());
buttons.add(previous);
buttons.add(Box.createRigidArea(buttonSpace));
// the next button displays the next flash card
next = new JButton("Next Card");
next.addActionListener(new NextListener());
buttons.add(next);
buttons.add(Box.createRigidArea(buttonSpace));
// the save button saves the current flash card
save = new JButton("Save Card");
save.addActionListener(new SaveCardListener());
buttons.add(save);
// create a filler between the answer box and buttons
content.add(Box.createRigidArea(fillerSize));
// Add buttons to the main panel
content.add(buttons);
// Add content to the window and pack it
setContentPane(content);
pack();
}
// Clear card clears the values from a card
public void clearCard() {
question.setText("");
answer.setText("");
}
// Display the next card, if there is one
public void displayNextCard() {
if (index < cards.size()) {
FlashCardData curCard = cards.get(index);
question.setText(curCard.getQuestion());
answer.setText(curCard.getAnswer());
}
else
clearCard();
}
private class PreviousListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (index != 0) {
index--;
FlashCardData curCard = cards.get(index);
question.setText(curCard.getQuestion());
answer.setText(curCard.getAnswer());
}
}
}
private class NextListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (index < cards.size()) {
index++;
displayNextCard();
}
}
}
private class SaveCardListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String questionText = question.getText();
String answerText = answer.getText();
FlashCardData card = new FlashCardData(questionText, answerText);
cards.add(index, card);
}
}
/***********************************************************************************
private class SaveListener implements ActionListener {
private FlashCardFrame toSave;
public SaveListener(FlashCardFrame toSave) {
this.toSave = toSave;
}
public void actionPerformed(ActionEvent e) {
saveFC = new JFileChooser();
try {
saveFC.setSelectedFile(new File("New file"));
int returnVal = saveFC.showSaveDialog(toSave);
if (returnVal == JFileChooser.APPROVE_OPTION) {
FileOutputStream fos = new FileOutputStream(saveFC.getSelectedFile());
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(toSave);
fos.close();
}
}
catch (FileNotFoundException fnfexc) {
System.out.println("File not found");
}
catch (IOException ioexc) {
System.out.println("IO Exception: " + ioexc.getMessage());
}
}
}
private class OpenListener implements ActionListener {
private FlashCardFrame openIn;
public OpenListener(FlashCardFrame openIn) {
this.openIn = openIn;
}
public void actionPerformed(ActionEvent e) {
openFC = new JFileChooser();
try {
int returnVal = openFC.showOpenDialog(openIn);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = openFC.getSelectedFile();
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
FlashCardFrame newFrame = (FlashCardFrame) ois.readObject();
}
}
catch (FileNotFoundException fnfexc) {
System.out.println("File not found");
}
catch (IOException ioexc) {
System.out.println("IO Exception: " + ioexc.getMessage());
}
catch (ClassNotFoundException cnfexc) {
System.out.println("Class not found exception");
}
}
}
/********************************************************************************************
private class RunListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
new FlashCardPlayer(cards);
}
}
public static void main(String[] args) {
new FlashCardFrame();
}
}