如何在 JList 中组合 2 个向量?
Vector<String> totalProducts = Products.getProductNames();
Vector<String> selectedProducts = Products.getSelectedProductNames();
selectedProducts 向量是 totalProducts 的子向量(这意味着 selectedProducts 包含 totalProducts 中的一个、多个或全部元素>)。我想要的是将这两个向量组合起来并生成一个JList,其中包含totalProducts 中的所有元素,以及已选择 的selectedProducts 元素。
我尝试过的:
Vector<Integer> indices = new Vector<Integer>();
JList prdList = new JList(totalProducts);
for(int i = 0; i < totalProducts.size(); i++)
{
for(String name : selectedProducts)
{
if(totalProducts.contains(name)) indices.add(i);
}
}
Object [] objIndices = indices.toArray();
//... Cast from Object [] to int [] ....
prdList.setSelectedIndices(intIndices);
...但这会选择最终 JList 中的所有元素。
以前我尝试过:
JList prdList = new JList(totalProducts);
for(String tName : totalProducts)
{
for(String sName : selectedProducts)
{
if(totalProducts.contains(sName)) prdList.setSelectedValue(sName, false);
}
}
...但是这个仅选择了 selectedProducts 中的最后一个元素。
你能帮我做对吗?
Vector<String> totalProducts = Products.getProductNames();
Vector<String> selectedProducts = Products.getSelectedProductNames();
The selectedProducts vector is a subvector of totalProducts (meaning that selectedProducts contains one, more or all of the elements from totalProducts). What I want is to combine these two vectors and make a single JList, which contains all the elements from totalProducts, and with the elements of selectedProducts already selected.
What I tried:
Vector<Integer> indices = new Vector<Integer>();
JList prdList = new JList(totalProducts);
for(int i = 0; i < totalProducts.size(); i++)
{
for(String name : selectedProducts)
{
if(totalProducts.contains(name)) indices.add(i);
}
}
Object [] objIndices = indices.toArray();
//... Cast from Object [] to int [] ....
prdList.setSelectedIndices(intIndices);
...but this selects all the elements in the final JList.
Previously I tried:
JList prdList = new JList(totalProducts);
for(String tName : totalProducts)
{
for(String sName : selectedProducts)
{
if(totalProducts.contains(sName)) prdList.setSelectedValue(sName, false);
}
}
...but this one selected only the last element from the selectedProducts.
Can you please help me to do it right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您之所以尝试选择所有项目,是因为您正在迭代每个项目,并且如果 selectedProducts 列表中的任何项目位于总列表中,则会将迭代项目的索引添加到最终选择列表中。尝试将循环更改为如下所示:
Your attempt that selects all items does so because you're iterating over each item, and if any item from the selectedProducts list is in the total list, adds the iteration item's index to the final selection list. Try changing your loop to something like this:
在调试你的第一次尝试时(看起来应该工作,你的intIndices数组的内容是什么?因为假设你的数组转换工作,它看起来应该工作。
但是,因为
selectedproducts
保证项目数少于总数,但您可能想对其进行迭代?因为
indexOf
是通过列表进行线性搜索,因此它可能不会产生太大影响。无论哪种方式都有所不同,对于您的第二次尝试,ListSelectionModel 具有添加选定索引的方法(
addSelectionInterval(int index0, int index1)
),您正在使用设置(覆盖)选择的那个。
请参阅http://download.oracle.com/javase/ 6/docs/api/javax/swing/ListSelectionModel.html
:您可能想使用
List<>
而不是Vector<>
,如下所示Vector 有很多不必要的同步开销。除非您需要同步....编辑修复了add(i)与add(index)的复制+粘贴
in debugging your first attempt (which looks like it should work, what was the contents of your intIndices array? because that looks like it should work, presuming your array conversion works.
however, since
selectedproducts
is guaranteed to be less items than total, you might want to iterate over that instead?although, since
indexOf
is a linear search through a list, it probably doesn't make much of a difference either way.as for your second attempt, the ListSelectionModel has methods for adding a selected index (
addSelectionInterval(int index0, int index1)
), you're using the one that sets (overwrites) the selection.
see http://download.oracle.com/javase/6/docs/api/javax/swing/ListSelectionModel.html
aside: you might want to use
List<>
instead ofVector<>
, as vector has a lot of unecessary synchronization overhead. Unless you need the synchronization....edit fixed copy+paste of add(i) with add(index)