简体   繁体   English

可以从JList中获取所有商品吗?

[英]Possible to get all Items from JList?

I know it is possible to get all selected Items from a JList with: 我知道可以通过以下方式从JList获取所有选定的项:

leftlist.getSelectedValues();

But is it possible to get all Items and not only the selected ones? 但是,是否有可能获得所有项目,而不仅是所选项目?

You need to use getModel() and getSize() to catch the length of the JList . 您需要使用getModel()getSize()来捕获JList的长度。 Also you need a loop to iterate through the indexes to get elements( getElementAt() ). 另外,您还需要循环遍历索引以获取元素( getElementAt() )。

Example: 例:

for(int i = 0; i< jList.getModel().getSize();i++){
    System.out.println(jList.getModel().getElementAt(i));
}

You are right, you can get selected values from jList.getSelectedValues() . 没错,您可以从jList.getSelectedValues()获取选定的值。 Also it is possible to get multiple selected elements in JList by jList.getSelectedValuesList() or listen for events on the list's ListSelectionModel . 也可以通过jList.getSelectedValuesList()获取JList的多个选定元素,或者侦听列表的ListSelectionModel上的事件。 Read the documentation for How to Use Lists . 阅读有关如何使用列表的文档。

Multiple selection demo from oracle . oracle的多选演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM