JList and ArrayList update

前端 未结 2 1529
粉色の甜心
粉色の甜心 2021-01-16 05:55

I would like to have example on how to update a JList when I add or remove elements from a ArrayList.

The ArrayList is part of Model class. The Model class is passe

2条回答
  •  佛祖请我去吃肉
    2021-01-16 06:10

    You need to use a ListModel to control adding and removing items from a JList. The tutorial is very useful: http://download.oracle.com/javase/tutorial/uiswing/components/list.html

    Here is some example code from the tutorial:

    listModel = new DefaultListModel();
    listModel.addElement("Jane Doe");
    
    listModel.insertElementAt(employeeName.getText(), index);    
    
    int index = list.getSelectedIndex();
    listModel.remove(index);
    

    If you have an arraylist you could build your own List Model around it.

提交回复
热议问题