想象一下,您有一個包含字符串和List的列表視圖。我想選擇列表視圖中的所有字符串,匹配List<String>
,但是,我試圖使用SelectionModel中的selectIndices函數,並且正確的整數出來,但它不會選擇它們,只有最後一個(與灰色)顏色。我希望他們都有灰色的顏色。JavaFX Listview - 多選編程
一個例子是:
Listview: Cat, Dog, Tiger, Gorilla, Monkey
List: Dog & Gorilla.
選擇狗&列表視圖中的大猩猩。 selectionMode是'MULTIPLE',SelectionModel是MultipleSelectionModel。
ObservableList<String> names = MainModel.getInstance().getGroupNames();
names.remove(group.getName());
listviewInheritance.setItems(names);
int[] indices = new int[group.getInheritance().size()];
List<String> inheriNames = group.getInheritance();
for(int i = 0; i < inheriNames.size(); i++) {
for(int j = 0; j < names.size(); j++) {
if(inheriNames.get(i).equals(names.get(j))) {
System.out.println("Inheri: " + inheriNames.get(i) + " | Name: " + names.get(j)); // test purpose
indices[i] = j;
}
}
}
if(indices.length > 0) {
System.out.println(Arrays.toString(indices));
listviewInheritance.getSelectionModel().selectIndices(-1, indices);
}
我在想什麼?
你嘗試過的某些代碼可能會幫助人們幫助你 – Rafalon
你是否已將'selectionMode'設置爲'MULTIPLE'? – fabian
是的。我可以用我的鼠標和控制按鈕選擇多行,但我想要編程。 –