从JList获取多个选定项 [英] Get multiple selected items from a JList

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

问题描述

我正在创建一个包含四个列表的屏幕.基本上是两对列表,您可以在该对列表中的一个列表上选择行,然后将其移至该对中的另一个列表.

I am creating a screen with four lists on it. Basically two pairs of lists where you can select lines on one list in the pair and move them to the other list in the pair.

在查看文档时,我需要为每个列表使用ListSelectionModel来确定已选择了哪些行.我将使用[Sel]或[Des]按钮进行实际的转移.

Looking at the documentation I need a ListSelectionModel for each list to determine which lines have been selected. I will use a [Sel] or [Des] button to do the actual transfer.

文档和样本说我需要一个ListSelectionListener,但是由于在用户单击按钮之前我不会访问模型,所以我实际上需要一个侦听器吗?如果我没有侦听器,模型是否还会设置getMinSelectionIndex,getMaxSelectionIndex和isSelectedIndex?

The documentations and samples say I need a ListSelectionListener but as I will not access the model until the user clicks on the button do I actually need a listener? Will the model still have the getMinSelectionIndex, getMaxSelectionIndex and isSelectedIndex set if I do not have a listener?

推荐答案

您不需要侦听器,侦听器仅用于使其他地方不需要的内容保持同步.

You do not need a listener, a listener is only useful for keeping something in sync elsewhere, which you don't need.

选择事件发生后,您可以随时访问所选索引.方法 JList.getSelectedIndices 返回当前选定索引的数组,而getSelectedValuesList()返回实际项,具体取决于您想要的内容.

You can access the selected indexes at any point after the selection event(s) occurs. The method JList.getSelectedIndices returns an array of currently selected indexes, and getSelectedValuesList() returns the actual items depending on what you want....

JList<String> items = new JList<String>(new String[] { "foo", "bar", "baz" });
// simulate selection
items.setSelectedIndices(new int[] { 0, 2 });

有时以后....

// get actual values
System.out.println(items.getSelectedValuesList());
// get indexes
System.out.println(Arrays.asList(items.getSelectedIndices()));

这篇关于从JList获取多个选定项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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