我有以下代码正常工作
made_list.setListData(original_list.getSelectedValues());
这里made_list是一个JList,original_list是另一个JList。如果我使用此代码运行,原始列表中的选定值将替换made_list中的先前值。我不想那样。我想改为追加..我该怎么办?
答案 0 :(得分:3)
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)
实现:
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