Adding list item to JList from another JList

删除回忆录丶 提交于 2019-12-22 10:29:16

问题


I have the following code working

made_list.setListData(original_list.getSelectedValues());

here made_list is one JList and original_list is another JList. If i run with this code the selected value from original_list is replacing the previous value in made_list. I dont want that. i want to append instead.. How do i do this ??


回答1:


1) Get the model for made_list
2) Get the selected items from orig_list
3) Make a new object[] that is the size of 1) + 2)
4) populate 3) with the items from 1) + 2)
5) set the make_list model with the object[] from 4)

Implementation:

ListModel made_model = made_list.getModel(); // 1

Object[] orig_sel = orig_list.getSelectedItems(); // 2

Object[] new_made_model = new Object[made_model.size() + orig_sel.length]; // 3

// this block is 4
int i = 0;
for(;i < made_model.size(); i++) 
    new_made_model[i] = made_model.getElementAt(i);
for(; i < new_made_model.length; i++) 
    new_made_model[i] = orig_sel[i - made_model.size());

made_model.setListData(new_made_model); // 5


来源:https://stackoverflow.com/questions/5098722/adding-list-item-to-jlist-from-another-jlist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!