java 如何使用 setSelectedValue 将多个项目设置为在 JList 中选择?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5961343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:41:05  来源:igfitidea点击:

How to set multiple items as selected in JList using setSelectedValue?

javaswingselectionjlist

提问by Niranjani S

I have a jList that is populated dynamically by adding to the underlying listModel. Now if I have three Strings whose value I know and I do

我有一个通过添加到底层 listModel 动态填充的 jList。现在,如果我有三个字符串,它们的值我知道并且我知道

for(i=0;i<3;i++){
    jList.setSelectedValue(obj[i],true);//true is for shouldScroll or not
}

only the last item appears to be selected...If this can't be done and I have to set the selection from the underlying model how should I go about it???

似乎只选择了最后一个项目......如果这不能完成并且我必须从基础模型中设置选择,我应该怎么做???

Also please note the jList has selection mode:

另请注意 jList 具有选择模式:

  jList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

Thanks in Advance

提前致谢

采纳答案by Riduidel

Although StanislasV answeris perfectly valid, i would prefer to avoid adding one selection interval to another. Instead, you should prefer to call the JListassociated ListSelectionModel#setSelectionInterval(int, int)method like this :

尽管StanislasV 的答案是完全有效的,但我宁愿避免将一个选择间隔添加到另一个。相反,您应该更喜欢像这样调用JList关联的ListSelectionModel#setSelectionInterval(int, int)方法:

jList.getSelectionModel().setSelectionInterval(0, 3)

If you want your list selection to be disjoint, you'll moreover have to write your own ListSelectionModel.

如果您希望您的列表选择不相交,您还必须编写自己的ListSelectionModel.

回答by kleopatra

Note that all xxSelectedValue methods are convenience wrapper methods around the selectionModel (which supports index-based selection access only) on the JList. Setting multiple selections per valueis not supported. If you really want it, you'll have to implement a convenience method yourself. Basically, you'll have to loop over the model's elements until you find the corresponding indices and call the index-based methods, something like:

请注意,所有 xxSelectedValue 方法都是围绕 JList 上的 selectionModel(仅支持基于索引的选择访问)的便利包装器方法。不支持为每个值设置多个选择。如果你真的想要它,你必须自己实现一个方便的方法。基本上,您必须遍历模型的元素,直到找到相应的索引并调用基于索引的方法,例如:

public void setSelectedValues(JList list, Object... values) {
    list.clearSelection();
    for (Object value : values) {
        int index = getIndex(list.getModel(), value);
        if (index >=0) {
            list.addSelectionInterval(index, index);
        }
    }
    list.ensureIndexIsVisible(list.getSelectedIndex());
}

public int getIndex(ListModel model, Object value) {
    if (value == null) return -1;
    if (model instanceof DefaultListModel) {
        return ((DefaultListModel) model).indexOf(value);
    }
    for (int i = 0; i < model.getSize(); i++) {
        if (value.equals(model.getElementAt(i))) return i;
    }
    return -1;
}

回答by StanislavL

jList.addSelectionInterval(0,2);