Ive made an address book. I can currently write to the arraylist and save it back to the .buab file, but I cant delete from the arraylist and export it back to the .buab file?
Im pretty much stuck on this. Im able to retrive contacts from the .buab and scroll through them using the JTextFields开发者_开发问答 and buttons ive created. Any help will be dearly appreciated.
Ive set up seprate classes for all operations (newcontacts, nextcontact etc).
If you need the code posted let me know.. Cheers
Dave
Deleting stuff from a list is as easy as adding:
Contact contact = new Contact();
myListOfContacts.add(contact); // adds a contact
myListOfContacts.remove(contact); // removes the contact
No more magic needed.
Edit
Please, and this is not Java, strictly separate three things, and your life will become much, much easier:
- The model - your contacts stored in your arraylist(s)
- The view - Your JFrame, showing the data from the model (the list(s))
- The Controller - The code behind your buttons that does something with the model (adding, removing, changing data on the array list)
Maybe you've heard of the MVC pattern (model-view-controller).
So you don't 'export your' JList: on your View (the JFrame), you press a button and some code from your Controller (actually what you might have in your buttons action listener) is executed to write the model (your arraylist(s)) to the file.
Same goes for deleting, adding, importing. You 'do' something on the view (the GUI) to affect changes to the model (the list(s)).
As I said, this is not Java, this is a very, very common pattern for object oriented languages. Look at your code, try to find the model, the view, the controller and try to separate the code. Promise, once you've done it, the next assignements will be a joke.
public void export(){ try { BufferedWriter fileOut = new BufferedWriter(new FileWriter( "contacts.buab", true)); fileOut.write(temp);
fileOut.close();
} catch (IOException ioe) {
JOptionPane.showMessageDialog(null, ioe.getMessage());
}
txtName.setText("");
txtHomeNum.setText("");
txtMobNum.setText("");
txtHomeAdd.setText("");
}
精彩评论