VBox内でHBoxを拡張するカスタムクラスのリストをソートしようとしています。すべてが型宣言を残して完全に動作していますが、警告を取り除く方法があるかどうか疑問に思っていました。 list
ObservableList<FilePane>
に設定しようとしカスタムクラスとコンパレータを使用する場合の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);
}
それが何getChildren()
戻りますので、それは<Node>
に設定する必要が私に告げるエラーが発生します。 <Node>
に設定すると、仕事とするのでFXCollections.sort(list, comp);
はFilePaneが動作しませんというエラーが発生しますしません:FilePaneはノード考慮しなければならないのHBoxを拡張
The method sort(ObservableList<T>, Comparator<? super T>) in the type FXCollections is not applicable for the arguments (ObservableList<Node>, Comparator<FilePane>)
?比較するクラスを持つ必要があるため、Comparatorの型をNodeに設定することはできません。 ObservableList<FilePane> list = (ObservableList<FilePane>) fileList.getChildren();
でキャスティングすると、それができないので、オプションではないことがわかります。
警告なしで正常に動作するので、タイプ警告を無視する必要がありますか? VBoxの子をObservableList<FilePane>
に設定する方法はありますか?
ありがとう!私はラムダを使用して最初の選択肢を使用し、警告がなくなった。 –