swing 修改JList中的选定元素
示例
给个JList喜欢
JList myList = new JList(items);
在列表中所选择的项目可通过被修改ListSelectionModel的 JList:
ListSelectionModel sm = myList.getSelectionModel(); sm.clearSelection(); //清除选择 sm.setSelectionInterval(index, index); //设置选择间隔 //(在这种情况下为单个元素)
另外,JList还提供了一些方便的方法来直接操纵所选索引:
myList.setSelectionIndex(index); //设置一个选定的索引 //可用于定义默认选择 myList.setSelectedIndices(arrayOfIndexes); //设置包含在其中的所有索引 //选定的数组