从另一个JList向JList添加列表项 [英] Adding list item to JList from another JList

查看:58
本文介绍了从另一个JList向JList添加列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码可用

made_list.setListData(original_list.getSelectedValues());

这里made_list是一个JList,original_list是另一个JList.如果我使用此代码运行,则从original_list中选择的值将替换made_list中的先前值.我不想要那个.我想改为附加..我该怎么做??

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) 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

这篇关于从另一个JList向JList添加列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆