Getting Kinda Desperate (No Tutorials Have Helped)

i dunno how to ask this question. ive asked it so many times and each time has been a failure.

ill try to explain it as best i can so please bear with me:

i am creating a phone book. i need to be able to save and load contacts to and from the phone book. i was told that serializing swing components was a bad idea, and that i should save each contacts information (name, number, email etc.) separately (i presume as a string). ive read the tutorial, on this website, about serialization but it did not help me in the situation that i was in.

im having a bit of trouble saving contact info but my biggest concern is that i dont know how ill be able to load each contacts info back into its original place, before the application was exited.

i hope that this explains my problem well enough. if u need any of my code just ask

any help would really be appreciate, thnx guys.

[910 byte] By [Alex1989a] at [2007-11-27 3:59:44]
# 1
This is what databases are for.
YoGeea at 2007-7-12 9:04:17 > top of Java-index,Java Essentials,Java Programming...
# 2
A database, maybe?
xiarcela at 2007-7-12 9:04:17 > top of Java-index,Java Essentials,Java Programming...
# 3
Is this an assignment where serialization was one of the requirements?
DrLaszloJamfa at 2007-7-12 9:04:17 > top of Java-index,Java Essentials,Java Programming...
# 4

A file, perhaps in some XMLish format? Or simply as a raw "flat" file?

((especially if the purpose is educational... if you have real world requirements, a database, of course, is best)).

Where have you gotten so far?

What is working?

Do you have a (SMALL) code sample to show us what you have tried?

Dave

xiarcela at 2007-7-12 9:04:17 > top of Java-index,Java Essentials,Java Programming...
# 5
How do you keep the phone subscribers in your phone book in the program? If they are in an ArrayList or other collection, serialize that, and everything it holds will go with it, and you'll be happy.
OleVVa at 2007-7-12 9:04:17 > top of Java-index,Java Essentials,Java Programming...
# 6

ok a database, ill read the tutorial on this site.

> Where have you gotten so far?

> What is working?

so far ive completed the layout of the phone book as well as a few other basic things. this is actually going to be my project for my cambridge matric year.

> Do you have a (SMALL) code sample to show us what you

> have tried?

i hope that this is small enough. its my save and load methods:

public void saveContacts(String saveName, String saveNumber, String saveEMail){

IOName = saveName;

IONumber = saveNumber;

IOEMail = saveEMail;

out = null;

try{

out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("files/contacts")));

out.writeObject(IOName);

out.writeObject(IONumber);

out.writeObject(IOEMail);

}catch(IOException e){

e.printStackTrace();

}finally{

if(out != null){

try{

out.close();

}catch(IOException e){

e.printStackTrace();

}

}

}

}

public void loadContacts(ContactPanel loadContactPanel){

IOContactPanel = loadContactPanel;

in = null;

try{

in = new ObjectInputStream(new BufferedInputStream(new FileInputStream("files/contacts")));

IOName = (String)in.readObject();

IONumber = (String)in.readObject();

IOEMail = (String)in.readObject();

IOContactPanel.setName(IOName);

IOContactPanel.setNumber(IONumber);

IOContactPanel.setEMail(IOEMail);

}catch(IOException e){

e.printStackTrace();

}catch(ClassNotFoundException e){

e.printStackTrace();

}finally{

if(in != null){

try{

in.close();

}catch(IOException e){

e.printStackTrace();

}

}

}

}

Alex1989a at 2007-7-12 9:04:17 > top of Java-index,Java Essentials,Java Programming...
# 7
That's it, three Strings? All this noise over saving three strings?
DrLaszloJamfa at 2007-7-12 9:04:17 > top of Java-index,Java Essentials,Java Programming...
# 8

> That's it, three Strings? All this noise over saving

> three strings?

nope its a lot more involved than that.

whenever a contact is added to the phone book his/her details are saved. but i dont know how im going to be able to load back each contacts details into their orignal jpanel (custom jpanel class)

Alex1989a at 2007-7-12 9:04:17 > top of Java-index,Java Essentials,Java Programming...
# 9

> How do you keep the phone subscribers in your phone

> book in the program? If they are in an ArrayList or

> other collection, serialize that, and everything it

> holds will go with it, and you'll be happy.

yea but if i do that then ill be serializing swing components which i hear is not a good idea.

Alex1989a at 2007-7-12 9:04:17 > top of Java-index,Java Essentials,Java Programming...
# 10

> > How do you keep the phone subscribers in your

> phone

> > book in the program? If they are in an ArrayList

> or

> > other collection, serialize that, and everything

> it

> > holds will go with it, and you'll be happy.

>

> yea but if i do that then ill be serializing swing

> components which i hear is not a good idea.

java.util.ArrayList is not a Swing component -- it is a collection:

http://java.sun.com/docs/books/tutorial/collections/index.html

Using it, you could serialized/deserialize a list of contact objects in one line of code.

DrLaszloJamfa at 2007-7-12 9:04:17 > top of Java-index,Java Essentials,Java Programming...
# 11

> java.util.ArrayList is not a Swing component -- it is

> a collection:

>

> http://java.sun.com/docs/books/tutorial/collections/in

> dex.html

>

> Using it, you could serialized/deserialize a list of

> contact objects in one line of code.

ill be adding jpanels to it

Alex1989a at 2007-7-12 9:04:17 > top of Java-index,Java Essentials,Java Programming...
# 12
Why are you adding the panels why not build an object underneath them to hold the information you want serialized put that into your collection and serialize the collection?PS.
puckstopper31a at 2007-7-12 9:04:17 > top of Java-index,Java Essentials,Java Programming...
# 13
> ill be adding jpanels to itSigh. I can't stop you from doing something silly. All I can do is point outwhat can work, if you use it correctly.
DrLaszloJamfa at 2007-7-12 9:04:17 > top of Java-index,Java Essentials,Java Programming...
# 14

You don't have to load it back into the SAME screen control (whatever form it takes). You load the information into a new instance of the screen control.

You don't EVER want to couple the data with the screen control displaying it.

Consider

public class Contact{

private String firstName ;

private String lastName ;

private String id ;

// Accessors and mutators as needed

}

Now

public class ContactPanel extends JPanel{

private Contact _thisContact ;

private JTextField _lastName ;

private JTextField _firstName ;

private JTextField _id ;

// Other methods as needed

}

and finally

public class ContactList implements Collection, Serializable{

private ArrayList _contacts

// Other methods as needed

}

The ContactList holds Contacts, the ContactPanel just displays information that's given to it.

Starting to make a little more sense maybe?

PS.

puckstopper31a at 2007-7-12 9:04:17 > top of Java-index,Java Essentials,Java Programming...
# 15
ok ill give that a try puckstopper31, thnx. but d amn all this stuff confuses the hell outa me! i should never have taken comp sci in the first place haha!Message was edited by: Alex1989
Alex1989a at 2007-7-21 21:00:27 > top of Java-index,Java Essentials,Java Programming...
# 16
Not trying to be mean or anything, but if this doesn't make sense to you, then you haven't internalized the previous work and that puts you at a serious disadvantage. One that only you can correct.PS.
puckstopper31a at 2007-7-21 21:00:27 > top of Java-index,Java Essentials,Java Programming...