简体   繁体   中英

JList - loop that adds elements to JList

I'm trying to make a loop that adds items to jlist but when I put in actionlistener it doesn't seem to be doing it...

textFieldSearch.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            for (int i = 0; i < 10; ++i) {
                locations[i] = "blah";
            }
        }
    });

you should try this :

textFieldSearch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                DefaultListModel model = (DefaultListModel) JLISTNAME.getModel();
                for (int i = 0; i < 10; ++i) {
                    model.add(i, "blah");
                }
            }
        });

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