Amin Behravesh
Amin Behravesh

Reputation: 374

How to show model's value in JList?

I have a ArrayList and I feel it with models,I want to show models value in Jlist,I pass arraylist to Jlist and convert it to array but I don't know how to display the value of each object. my code is here:

ListIterator<PhoneModel> listIterator = list.listIterator();

while (listIterator.hasNext()) {
    PhoneModel value = listIterator.next();
    System.out.println("The phone number of "+value.name+" is "+value.number);
}
jList.setListData(list.toArray());

thanks for any help my friends!

Upvotes: 2

Views: 362

Answers (2)

user3133177
user3133177

Reputation: 33

Use DefaultListModel to get data from iterator. then create new defaultlistmodel runtime and add this model.addElement(yourvariable.ToString());

For Eg. here it takes Listarray..

DefaultListModel<String> model;

private void UpdateJList()
{
    model = new DefaultListModel<String>();
    for(Person p : personList){
     model.addElement(p.ToString());
}    
clientJList.setModel(model);     
clientJList.setSelectedIndex(0);

}

model data insert into jList..

Upvotes: 1

kosta
kosta

Reputation: 406

You can override toString() method on PhoneModel, so it automatically displays it as label in JList.

Upvotes: 2

Related Questions