我正在编写一个通用 Java 类来显示可以取消/选中的过滤器项目列表,然后将结果选择传递给控制器以过滤主要对象的集合以进行显示。我正在使用两个 ControlsFX 控件:PopOver
包含过滤器列表的控件和CheckListView
显示和控制单个项目的控件。
流程是PopOver
在控制器的构造函数中实例化过滤器类,并且每次显示时重新加载源数据。
从表面上看,代码似乎可以工作。添加了源项目,选中和取消选中工作并且选中项目列表中的项目数正确。但是,我已将侦听器添加到源数据和检查项目列表的可观察列表中,并且两者都没有被触发。
我尝试将侦听器内联并作为单独的方法(如示例中所示)编写,并且尝试以传统onChanged
和 lambda 样式编写它们。两者都没有改变。我也尝试过使用几乎相同的代码,只是将CheckListView
用作窗格的节点。这确实按预期工作。
我还浏览了 ControlsFX 问题日志,没有发现任何直接相关的内容(除了确认我使用的代码似乎没问题)。
这是过滤器类:
package debuglogger;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.layout.BorderPane;
import org.controlsfx.control.CheckListView;
import org.controlsfx.control.PopOver;
public class FilterPopOverSO<T> extends PopOver {
private ObservableList<T> sourceData;
private BorderPane popoverNode;
private CheckListView<T> filterList;
/**
* ============================================================================================
* Constructor
* ============================================================================================
*/
public FilterPopOverSO() {
super();
sourceData = FXCollections.observableArrayList();
popoverNode = new BorderPane();
filterList = new CheckListView<>();
filterList.setPrefWidth(600D);
filterList.setMaxWidth(600D);
popoverNode.setCenter(filterList);
setContentNode(popoverNode);
filterList.getItems().addListener(sourceItemsListener);
filterList.getCheckModel().getCheckedItems().addListener(checkedItemsListener);
filterList.getCheckModel().checkAll();
}
/**
* ============================================================================================
* Listeners
* ============================================================================================
*/
ListChangeListener<T> sourceItemsListener = (change) -> {
System.out.println("Start of Change Listener 'sourceItemsListener'");
while(change.next()) {
System.out.println(" Added: " + change.wasAdded());
System.out.println(" Permutated: " + change.wasPermutated());
System.out.println(" Removed: " + change.wasRemoved());
System.out.println(" Updated: " + change.wasUpdated());
}
System.out.println("End of of Change Listener 'sourceItemsListener'");
};
ListChangeListener<T> checkedItemsListener = (change) -> {
System.out.println("Start of Change Listener 'checkedItemsListener'");
while(change.next()) {
System.out.println(" Added: " + change.wasAdded());
System.out.println(" Permutated: " + change.wasPermutated());
System.out.println(" Removed: " + change.wasRemoved());
System.out.println(" Updated: " + change.wasUpdated());
}
System.out.println("End of of Change Listener 'checkedItemsListener'");
};
public void setSourceData(ObservableList<T> sourceData) {
filterList.setItems(sourceData);
}
}
这是测试存根:
package scratchpad;
import debuglogger.FilterPopOverSO;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class FilterPopOverTest extends Application {
@Override
public void start(Stage primaryStage) {
ObservableList<String> sourceList = FXCollections.observableArrayList("Item 1", "Item 2", "Item 3");
FilterPopOverSO<String> p = new FilterPopOverSO<>();
AnchorPane root = new AnchorPane();
Button btn = new Button("Click Me!");
btn.setOnAction(event -> {
System.out.println("Just to show that I was here!");
p.setSourceData(sourceList);
p.show(btn);
});
root.getChildren().add(btn);
AnchorPane.setTopAnchor(btn, 50D);
AnchorPane.setLeftAnchor(btn, 50D);
primaryStage.setTitle("Filter PopOver Test");
primaryStage.setScene(new Scene(root, 500, 500));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}