The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
February 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Why isn'y my code opening a Saved file ?

Posted by Avin Sinanan on February 18, 2002 at 9:43 PM

Hello,
I've written some code that create a frame, panel and a menubar.
Teh menubar has a "File" menu that contains a the following menu item -

Save
Open
Add a Button

The "Add a Button" menuitem adds a button to the Panel. The save button saves it and well the open button is suppose to open back up a saved file. I think the save part is working because when I save the file after I add a few buttons I see the name of the file that is saved. But when its time to open back up that file it isn't opening it. Could someone tell me what am doing wrong..

Oh and thanks Allard van Hooff for the advice ealier.

Ok thanks for reading.... Any help would be greatly appreciated...

Ok here is the CODE -->

import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.io.*;
import java.util.zip.*;
import java.util.Vector;
import java.util.Properties;
import javax.swing.*;

class AvinSave
{
public static void main(String[] args)
{
MainProgram mainProgram = new MainProgram();
}
}

class MainProgram implements Serializable
{
static public int i =0;
JFrame frame = new JFrame();
JPanel pane = new JPanel();
JMenuBar menubar = new JMenuBar();
JMenu file = new JMenu("File");
private Vector buttons = new Vector();

public MainProgram()
{


JMenuItem save = new JMenuItem("Save");
save.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
save();

}
});



JMenuItem open = new JMenuItem("Open");
open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
load();
}
});



JMenuItem addButton = new JMenuItem("Add a Button");
addButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JButton button = new JButton(""+ i);
i++;
buttons.add(button);
pane.add(button);
pane.updateUI();

}
});


file.add(save);
file.add(open);
file.add(addButton);
menubar.add(file);
frame.setJMenuBar(menubar);

frame.getContentPane().add(pane);

frame.setSize(500,500);
frame.setVisible(true);
}

public void save() {
// Create a file dialog to query the user for a filename.
FileDialog f = new FileDialog(frame, "Save Scribble", FileDialog.SAVE);
f.show(); // Display the dialog and block.
String filename = f.getFile(); // Get the user's response
if (filename != null) { // If user didn't click "Cancel".
try {
// Create the necessary output streams to save the scribble.
FileOutputStream fos = new FileOutputStream(filename); // Save to file
GZIPOutputStream gzos = new GZIPOutputStream(fos); // Compressed
ObjectOutputStream out = new ObjectOutputStream(gzos); // Save objects
out.writeObject(buttons); // Write the entire Vector of scribbles
out.flush(); // Always flush the output.
out.close(); // And close the stream.
}
// Print out exceptions. We should really display them in a dialog...
catch (IOException e) { System.out.println(e); }
}
}


public void load() {
// Create a file dialog to query the user for a filename.
FileDialog f = new FileDialog(frame, "Load Scribble", FileDialog.LOAD);
f.show(); // Display the dialog and block.
String filename = f.getFile(); // Get the user's response
if (filename != null) { // If user didn't click "Cancel".
try {
// Create necessary input streams
FileInputStream fis = new FileInputStream(filename); // Read from file
GZIPInputStream gzis = new GZIPInputStream(fis); // Uncompress
ObjectInputStream in = new ObjectInputStream(gzis); // Read objects
// Read in an object. It should be a vector of scribbles
Vector newButtons = (Vector)in.readObject();
in.close(); // Close the stream.
buttons = newButtons; // Set the Vector of lines.
pane.repaint(); // And redisplay the scribble.
}
// Print out exceptions. We should really display them in a dialog...
catch (Exception e) { System.out.println(e); }
}
}


}


Yours repectfully, Avin Sinanan



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us