The Artima Developer Community
Sponsored Link

Java Answers Forum
How to save properly ?

1 reply on 1 page. Most recent reply: Mar 2, 2002 6:50 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Avin Sinanan

Posts: 28
Nickname: avin
Registered: Feb, 2002

How to save properly ? Posted: Mar 2, 2002 7:31 AM
Reply to this message Reply
Advertisement
Hello I created some code to add draggable buttons to a JPanel.
Every added button can be moved around once added to the JPanel. 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 appears back on the screen you cannot drag the buttons around again.. Can you help me please. Am really inexperinced in saving and opeing stuff. I tried reading tutorials on the net but its not a big help. Sorry for being a pest. Anyway here is the code.. and thanks for even looking at it...
Yours respectfully Avin Sinanan.. 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.*;
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); }
}
}
 
 
}


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: How to save properly ? Posted: Mar 2, 2002 6:50 PM
Reply to this message Reply
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); 
			}
		}
	}
 
}

Flat View: This topic has 1 reply on 1 page
Topic: Query Previous Topic   Next Topic Topic: Please help with spokes

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use