aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Why isn't my code Opening a Saved file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Why isn Watch "Why isn New topic
Author

Why isn't my code Opening a Saved file?

Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
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
[ February 18, 2002: Message edited by: Avin Sinanan ]

yours respectfully<br />Avin Sinanan
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Avin,
Your file I/O is working correctly. Your problem is what you do with the file contents. You probably want to view the buttons (that were saved into the file) on the panel. To perform that you need to do more than placing the file contents into a vector. You will need to use the vector contents and add the components into the panel. The code below should replace your line:
buttons = newButtons;

Then the buttons will magically appear on the panel.
NOTE: YOu should not be calling UpdateUI for replacing some components. It should be used to change the Look and Feel which doesn't seem to be your case at all. You should replace every updateUI call with the correct one for your case: validate.
Regards,
Manfred.
[ February 19, 2002: Message edited by: Manfred Leonhardt ]
Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
Hey Manfred thanks very much for the help. It works. Thanks a million!
Yours repspectfully Avin Sinanan
Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
Hey Manfred, I ran into another problem. The cod eyou gave me worked perfectly. However I altered my code to do the following - Every added button can now be moved around. That is, when every you add a button you can now move it around. It saves and everything. However when you opened back a saved file the buttons cannot be moved. When the saved button appear back on the screen you cannout drag the buttons around again.. Can you help me one more time please. Am really inexperinced in saving and opeing stuff. I tried reading tutorials on te net but its not a big help. Sorry for being a pest. Anyway here is the code.. and thnaks for even looking at it...
Yours respectfully Avin Sinanan.. here is the altered code..
It very very very close to the old code.. the only difference is that the buttons can be dragged...
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.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.*;
import javax.swing.event.*;
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()
{
pane.setLayout(null);
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);
button.setBounds(10,90,80,50);
button.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent me) {
me.getComponent().setLocation(me.getComponent().getX() + me.getX(),
me.getComponent().getY() + me.getY());

}
});
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.

pane.removeAll();
for( int i = 0; i < newButtons.size(); ++i )
{
pane.add( (JButton)newButtons.elementAt( i ));
} // 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); }
}
}

}
[ February 19, 2002: Message edited by: Avin Sinanan ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why isn't my code Opening a Saved file?