0

I'm trying to update the JList by deleting a contact and its not updating the JList like I wanted it to.

Originally, the JList would look like this.

Broadcast //Not allowed to delete this element
Andy
Bobby
Chris
Brad

Assuming that I have deleted Andy off the list using a button. It should show

Broadcast //Not allowed to delete this element
Bobby
Chris
Brad

Instead it shows this on the JList.

contact0
contact2
contact3
contact4

I don't know what went wrong and I looked in many sources. I want it to return the actual names, not the contact#. Code is below.

private JList<String> listContacts;
private DefaultListModel<String> model;
private String[] contactList;

model = new DefaultListModel<String>();                                                             
contactList = controller.getContacts();
for(String contact : contactList){
    model.addElement(contact);
}
listContacts = new JList<String>();                                                                  
listContacts.setModel(model);

class MyButtonListener5 implements ActionListener{
    public void actionPerformed(ActionEvent e){
        if(listContacts.getSelectedIndex() == 0){
            controller.displayMsg("Can't delete broadcast.\n");
        }
        else if(listContacts.getSelectedIndex() > 0){
            controller.displayMsg("You tried to delete " + listContacts.getSelectedValue() +".\n");
            model.remove(listContacts.getSelectedIndex());
            listContacts.setModel(model);
        }
        else{
            controller.displayMsg("No contact to delete. Please select one.\n");
        }
    }
}

This is for contactList to getContacts. This method is in the controller class. I hope this clarifies why I'm having trouble.

public String[] getContacts(){
    Contact[] contacts = new Contact[5];
    String[] names = new String[5];
    for(int i = 0; i < 5; i++){
        contacts[i] = new Contact("contact" + i);
        names[i] = contacts[i].getName();
    }
    return names;
}

This is in my Contact Class.

private String name;
private String nickname;

public Contact(){
    name = "";
    nickname = "";
}
public Contact(String name){
    this.name = name;
}
public void setName(String name){
    this.name = name;
}
public String getName(){
    return name;
}
@Override
public String toString(){
    return this.getName();
}

1 Answer 1

1

I think you can fix this by overriding the toString() method in your Contact class.

@Override
public String toString() {
    return this.getName();
}

Then you can return your contacts without .getName() and just directly add them to the list. I recommend you taking a look at this example.

private JList listContacts;
private DefaultListModel model;
private Contact[] contactList;

model = new DefaultListModel();                                                             
contactList = controller.getContacts();
for(Contact contact : contactList){
    model.addElement(contact);
}
listContacts = new JList();                                                                  
listContacts.setModel(model);

class MyButtonListener5 implements ActionListener{
    public void actionPerformed(ActionEvent e){
        if(listContacts.getSelectedIndex() == 0){
            controller.displayMsg("Can't delete broadcast.\n");
        }
        else if(listContacts.getSelectedIndex() > 0){
            controller.displayMsg("You tried to delete " + listContacts.getSelectedValue() +".\n");
            model.remove(listContacts.getSelectedIndex());
            listContacts.setModel(model);
        }
        else{
            controller.displayMsg("No contact to delete. Please select one.\n");
        }
    }
}

And your getContacts() method:

public Contact[] getContacts(){
    Contact[] contacts = new Contact[5];
    contacts[0] = "Broadcast";
    contacts[1] = "Andy";
    contacts[2] = "Bobby";
    contacts[3] = "Chris";
    contacts[4] = "Brad";

    return contacts;
}
Sign up to request clarification or add additional context in comments.

8 Comments

If I already did that, how do I modify the existing code I have in this post? Or do I not need to?
You need to change your getContact method to return type Contact and return the contacts array. Also you need to change your DefaultListModel i guess... Can you post your Contact() class? It would be extremly helpful...
Ok, I'll edit my answer :D Please update your answer and add your contact class...
I updated my answer according to yours, and it turns out that it doesn't access contacts class. I included it and I got a new error. incompatible types: String[] cannot be converted to Contact[] [javac] contactList = controller.getContacts();
I guess you forgot the return type of your getContacts() method: public Contact[] getContacts()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.