tags:

views:

69

answers:

4

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

A: 

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:

  1. The model - your contacts stored in your arraylist(s)
  2. The view - Your JFrame, showing the data from the model (the list(s))
  3. 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.

Andreas_D
and before closing the application don't forget the save ArrayLists in file
Nettogrof
the contact information will be shown through the JTextFields, so the current contact information in the index will need to be deleted.Sorry im as thick as they come with java
Dave
@Dave, you can delete from a ArrayList using the index, like txtNum.remove(index);
Nettogrof
@nettogrof, error messages have come up, see below
Dave
A: 

@Nettogrof. Ive placed it in but its giving me errors.

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 2
 at java.awt.Container.remove(Unknown Source)
 at BasicGui.delete(BasicGui.java:340)
 at BasicGui.actionPerformed(BasicGui.java:149)
 at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
 at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
 at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
 at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
 at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
 at java.awt.Component.processMouseEvent(Unknown Source)
 at javax.swing.JComponent.processMouseEvent(Unknown Source)
 at java.awt.Component.processEvent(Unknown Source)
 at java.awt.Container.processEvent(Unknown Source)
 at java.awt.Component.dispatchEventImpl(Unknown Source)
 at java.awt.Container.dispatchEventImpl(Unknown Source)
 at java.awt.Component.dispatchEvent(Unknown Source)
 at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
 at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
 at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
 at java.awt.Container.dispatchEventImpl(Unknown Source)
 at java.awt.Window.dispatchEventImpl(Unknown Source)
 at java.awt.Component.dispatchEvent(Unknown Source)
 at java.awt.EventQueue.dispatchEvent(Unknown Source)
 at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.run(Unknown Source)
Dave
Dave, if possible, edit your original post to add more information
Andreas_D
This kind of error ( java.lang.ArrayIndexOutOfBoundsException ) can happen when you try example to remove the 5th element, but your ArrayList have only 2 element. To be able to help with your specific code, we will need the important part ( not all) of your code
Nettogrof
Its ok ive managed to delete from my array, now I need to be able to write the updated arraylist to the buab... help?
Dave
Like your export functionality, just write the whole list and do not append but overwrite.
Andreas_D
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(""); }
Dave
A: 

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("");


}
Dave
instead fileOut.wirte(temp) do a loop to write your arraylists data.
Nettogrof
A: 

Can anyone help?

Dave
I know that your assignment is for the end of the month, but here it's not a chat room, and like Andreas_d told you, you can edit your post instead of adding "answer"
Nettogrof