import javax.swing.*; import java.awt.event.*; import java.io.*; public class FileOpenDialog extends JFileChooser implements ActionListener { WorkSpace workSpace; DrawingCanvas canvas; public FileOpenDialog (WorkSpace workSpace, DrawingCanvas canvas) { this.workSpace = workSpace; this.canvas = canvas; } public void actionPerformed(ActionEvent e) { File path = null; try { showOpenDialog(null); path = getSelectedFile(); FileInputStream inFile = new FileInputStream(path); ObjectInputStream inObj = new ObjectInputStream(inFile); workSpace.setShapes((WorkSpace) inObj.readObject()); canvas.repaint(); } catch (NullPointerException npe) { // Just ignore this; it means the user didn't select a file. } catch (FileNotFoundException fnfe) { new JOptionPane(). showMessageDialog(null, "File " + path + " not found.", "", JOptionPane.ERROR_MESSAGE); } catch (IOException ioe) { new JOptionPane(). showMessageDialog(null, "I/o exception:" + ioe, "", JOptionPane.ERROR_MESSAGE); } catch (ClassNotFoundException cnfe) { new JOptionPane(). showMessageDialog(null, "Class not found:" + cnfe, "", JOptionPane.ERROR_MESSAGE); } } }