'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
?
Solution 1:[1]
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>()
.
Solution 2:[2]
Don't create an
ArrayList
ofContacts
. All theContact
data should be stored in theListModel
.So when you click on the button all you do is create a
Contact
and add theContact
to theDefaultListModel
. However, your code doesn't make much sense because you're just creating an empty Contact. I would expect theContact
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 aContact
using the name data and then add theContact
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.
Solution 3:[3]
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");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | durron597 |
Solution 2 | durron597 |
Solution 3 | Kirby |