我正在嘗試對VBox內部擴展HBox的自定義類的列表進行排序。一切正在完美地完成,而忽略了類型聲明,但我想知道是否有辦法擺脫警告。使用自定義類和比較器時ObservableList的設置類型
public static class FilePane extends HBox {}
public void sort() {
int order = orderBy.getSelectionModel().getSelectedIndex();
Comparator<FilePane> comp = null;
if(order == 0) {
comp = Comparator.comparing(FilePane::getFileNameLower);
} else if(order == 1) {
comp = Comparator.comparingLong(FilePane::getFileDate);
comp = comp.reversed();
} else if(order == 2) {
comp = Comparator.comparingLong(FilePane::getFileSize);
comp = comp.reversed();
} else if(order == 3) {
comp = Comparator.comparingLong(FilePane::getFileCount);
comp = comp.reversed();
} else if(order == 4) {
comp = Comparator.comparing(FilePane::getDirectory);
comp = comp.reversed();
}
ObservableList list = fileList.getChildren();
FXCollections.sort(list, comp);
}
嘗試設置list
到ObservableList<FilePane>
給出了一個錯誤,告訴我,因爲那是什麼getChildren()
返回時,就應設置爲<Node>
。將其設置爲<Node>
不起作用,FXCollections.sort(list, comp);
給出了一個錯誤FilePane不會起作用,因爲:
The method sort(ObservableList<T>, Comparator<? super T>) in the type FXCollections is not applicable for the arguments (ObservableList<Node>, Comparator<FilePane>)
FilePane擴展HBox中應該考慮的一個節點?比較器的類型不能設置爲節點,因爲它需要與類進行比較。與ObservableList<FilePane> list = (ObservableList<FilePane>) fileList.getChildren();
鑄造告訴我,它不能這樣做,所以它不是一個選項。
我應該忽略類型警告,因爲它沒有它們可以正常工作嗎?有沒有辦法將VBox的孩子設置爲ObservableList<FilePane>
?
謝謝!我使用lambda的第一個選擇,警告消失了。 –