Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: How to save properly ?
|
Posted: Mar 2, 2002 6:50 PM
|
|
I modified your code between the two //************************************* lines. I believe I got your program working like you wanted. I was able to save and reload buttons and move them all around after loading.
Charles See the modified code below: I added indents for my own benefit. You should be able to copy and paste the part I mdified into your program and get it working.
Additional problems: email me at charbell@bellsouth.net
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 ){
for (int n = 0;n < newButtons.size();n++){
JButton nextbutton = (JButton)newButtons.elementAt(n);
nextbutton.setBounds(10,90,80,50);
nextbutton.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent me) {
me.getComponent().setLocation(me.getComponent().getX() + me.getX(),
me.getComponent().getY() + me.getY());
}
});
pane.add(nextbutton);
i = n;
System.out.println("loaded button " + n);
//pane.add( (JButton)newButtons.elementAt( i ));
} // Set the Vector of lines.
pane.repaint(); // And redisplay the scribble.
i++;
buttons = newButtons;
//*************************************
}
// Print out exceptions. We should really display them in a dialog...
catch (Exception e) {
System.out.println(e);
}
}
}
}
|
|