简体   繁体   中英

Adding element to JList does not work

I'm trying to add a new element to my empty JList but it doesn't appear. I'm running it on the EDT (I've very low information on that subject)

public void populateProjectsList(List<Project> projectsList2) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            DefaultListModel<String> listModel = (DefaultListModel) projectsList.getModel();
            listModel.clear();
            for (Project project : projectsList2) {
                System.out.println(project.id + ") " + project.name);
                listModel.addElement(project.id + ") " + project.name);
            }
        }
    });
}

System.out.println works, I see the correct output that I wish to be added but the list is still empty as it is. I tried listModel.addElement in an action listener and it worked.

Your code works fine for instance when called from ListSelectionListener, see below.

My guess is that you add the items correctly to JList, but they are not seen, because the JList has so small dimensions, that its content cannot be seen. Work on the layout of the components in your window, or put some data into JList at the time you add it to the window. It may well be some other reason, of course.

A working example

import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Main 
{
    public static void main(String args[])
    {
        new Main();
    }

    public Main()
    {
        JFrame fr = new JFrame("Hello");

        DefaultListModel<String> model = new DefaultListModel<String>();
        model.addElement("hello");
        model.addElement("hi");
        model.addElement("ola");

        projectsList = new JList<>(model);
        fr.add(projectsList);
        fr.pack();
        fr.setVisible(true);

        projectsList.addListSelectionListener(
                new ListSelectionListener() 
                {
                    @Override
                    public void valueChanged(ListSelectionEvent e) 
                    {
                        List<Project> toAdd = new ArrayList<>();
                        toAdd.add(new Project(7, "seven"));
                        toAdd.add(new Project(10, "ten"));
                        toAdd.add(new Project(4, "four"));
                        populateProjectsList(toAdd);
                        projectsList.removeListSelectionListener(this);                
                    }
                }
        );      
    }

    JList projectsList;

    private void populateProjectsList(List<Project> projectsList2) 
    {
        EventQueue.invokeLater(
            new Runnable() {
                @Override
                public void run() 
                {
                    DefaultListModel<String> listModel = (DefaultListModel<String>) projectsList.getModel();
                    listModel.clear();
                    for (Project project : projectsList2) 
                    {
                        System.out.println(project.id + ") " + project.name);
                        listModel.addElement(project.id + ") " + project.name);
                    }                    
                }
            }
        );
    }

    private class Project 
    {
        int id;
        String name;

        public Project(int id, String name) 
        {
            this.id = id;
            this.name = name;
        }        
    }
}

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