简体   繁体   中英

How to dynamically add items to a JList?

I'm having trouble adding items to a JList called lstContacts whenever a button is pressed.

When I press my new contact button, a line should be added to lstContacts but when I press the button nothing happens.

Here is my action listener for the new contact button:

newContactButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Contacts contacts = new Contacts();

            contacts.createContact();
        }
    });

Here is my createContact method in the contacts class:

ArrayList<String> contactsList = new ArrayList<String>();
public void createContact() {
    ContactsDB database = new ContactsDB();
    System.out.println("new contact");
    contactsList.add("New Contact");
    listModel.addElement("New Contact");
}

And lastly here is my createCustomUIComponent() method:

private void createUIComponents() {
    // TODO: place custom component creation code here
    listModel = new DefaultListModel();
    lstContacts = new JList<String>(listModel);
}

Why aren't the new contacts getting added to my JList ?

  1. Don't create an ArrayList of Contacts . All the Contact data should be stored in the ListModel .

  2. So when you click on the button all you do is create a Contact and add the Contact to the DefaultListModel . However, your code doesn't make much sense because you're just creating an empty Contact. I would expect the Contact to have a name or something. So really what you want to do is have a text field were the user can enter a name and then when you click the button you create a Contact using the name data and then add the Contact to the list.

Read the section from the Swing tutorial on How to Use Lists . The ListDemo example show you how to dynamically add/remove an item from the DefaultListModel . Download the example and modify the working example to meet your specific requirements. When learning a new concept, start with working code.

The problem is here:

Contacts contacts = new Contacts();

contacts.createContact();

You are creating a new contacts object every time you click the button, but the JList doesn't have a reference to the Contacts object. I don't know how the contacts object is defined, but you should make sure that the object you are adding the contact to has reference to the DefaultListModel .

Another problem I see (but isn't causing the problem you posted about) is that you call new DefaultListModel() , but list model is a parameterized type. You probably want to call new DefaultListModel<String>() .

First of all you have to have new a default ListView :

 DefaultListModel listModel = new DefaultListModel();

Thenm build your JList with this list:

 JList jList = new JList(listModel);

Anytime you want to change your list for each action you considered, call addElement on your jList:

listModel.addElement("This is a test contact");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM