简体   繁体   English

使用自定义类和Comparator时设置ObservableList的类型

[英]Setting type of ObservableList when using a custom class and Comparator

I am attempting to sort a list of a custom class extending HBox inside a VBox. 我正在尝试对在VBox内扩展HBox的自定义类的列表进行排序。 Everything is working perfectly leaving out the type declarations but I was wondering if there was a way to get rid of the warnings. 一切工作都很好,没有类型声明,但是我想知道是否有一种方法可以消除警告。

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);
}

Trying to set list to ObservableList<FilePane> gives an error telling me it should be set to <Node> since that's what getChildren() returns. 尝试将list设置为ObservableList<FilePane>会出现错误,告诉我应将其设置为<Node>因为那是getChildren()返回的内容。 Setting it to <Node> does not work and FXCollections.sort(list, comp); 将其设置为<Node>无效,并且FXCollections.sort(list, comp); gives an error that FilePane will not work because: 给出一个错误,指出FilePane将不起作用,因为:

The method sort(ObservableList<T>, Comparator<? super T>) in the type FXCollections is not applicable for the arguments (ObservableList<Node>, Comparator<FilePane>)

FilePane extends HBox which should be considered a Node? FilePane扩展了应该被认为是Node的HBox? Comparator's type can't be set to Node because it needs to have the class to compare with. 比较器的类型不能设置为Node,因为它需要具有要比较的类。 Casting with ObservableList<FilePane> list = (ObservableList<FilePane>) fileList.getChildren(); 使用ObservableList<FilePane> list = (ObservableList<FilePane>) fileList.getChildren(); tells me that it can't do that so it isn't an option. 告诉我它不能做到这一点,所以这不是一个选择。

Should I just ignore the type warnings since it works fine without them? 我应该忽略类型警告,因为没有它们就可以正常工作吗? Is there a way to set the VBox's children to a ObservableList<FilePane> ? 有没有一种方法可以将VBox的子级设置为ObservableList<FilePane>

If getChildren() returns a list of Node s, then you need the list to be a list of Node s. 如果getChildren()返回Node的列表,那么您需要将该列表作为Node的列表。 Let's start there and work backwards. 让我们从那里开始并向后工作。

ObservableList<Node> list = fileList.getChildren();

Okay. 好的。 If that's the list, then how do we get the sort() call to compile? 如果那是列表,那么我们如何获得sort()调用进行编译? The answer is that the comparator must be a Comparator<Node> and not a Comparator<FilePane> . 答案是比较器必须是Comparator<Node>而不是Comparator<FilePane>

But that's no good, right? 但这不好,对吗? Because then we can't use those really slick FilePane:: method references. 因为那样我们就不能使用那些真正漂亮的FilePane::方法引用。 Hold on there, partner. 等一下,伙伴。 Not so fast. 没那么快。 Isn't there some way to leave that code alone and still make sort() happy? 难道没有什么办法可以让该代码保持不变,并使sort()感到高兴吗?

There is. 有。 Let's leave comp alone. 让我们离开comp孤单。 It can stay as a Comparator<FilePane> . 它可以保留为Comparator<FilePane> What we need to do is convert it into a Comparator<Node> some how. 我们需要做的是将其转换为Comparator<Node>

FXCollections.sort(list, convertSomeHow(comp));

How do we convert it? 我们如何转换它? Well, sort() is going to pass the comparator Node s. 好了, sort()将通过比较器Node The comparator we have wants FilePane s. 我们需要的比较器是FilePane So all we need to do is get a cast in there and then defer to comp . 因此,我们所需要做的就是在其中进行转换,然后推迟进行comp Something like this: 像这样:

FXCollections.sort(list, Comparator.comparing(node -> (FilePane) node, comp));

Or: 要么:

FXCollections.sort(list, Comparator.comparing(FilePane.class::cast, comp));

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

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