how to bind ArrayList to JList

后端 未结 5 731
花落未央
花落未央 2020-12-10 18:44

i have a JList and an ArrayList.How to bind the datas in arraylist to the jlist.Are the any alternative methods?

    ArrayList arl = new ArrayList();
    ar         


        
相关标签:
5条回答
  • 2020-12-10 19:02

    You don't need to clone the ArrayList. Just call toArray()

    JList list = new JList(arl.toArray()); 
    
    0 讨论(0)
  • 2020-12-10 19:03
    ArrayList<String> aList = new ArrayList<String>();
    aList.add("blabla");
    aList.add("blublu");
    aList.add("blibli");
    aList.add("bleble");
    DefaultListModel<String> model = new DefaultListModel<String>();
    for(String s:aList){
        model.addElement(s);
    }
    JList<String> contactList = new JList<String>(model);
    

    Advantage of this is that you can later add/remove elements to already instantiated JList by adding elements to model using methods addElement(Obj o) and removeElement(Obj o).

    0 讨论(0)
  • 2020-12-10 19:20
    JList list = new JList(arl.toArray());
    
    0 讨论(0)
  • 2020-12-10 19:23
    JList jList = new JList(arrayList.toArray());
    
    0 讨论(0)
  • 2020-12-10 19:27

    Another option is:

    DefaultListModel Jlista = new DefaultListModel();
    
    public Form1() {
    
     jList1.setModel(Jlista);
    
    }
    
    public void yourFunction(){
     Jlista.addElement("newElement");
    }
    
    0 讨论(0)
提交回复
热议问题