nov*_*uff 0 java javafx scenebuilder
好的,所以我对我的 java 非常生疏,甚至在 javafx 上更是如此。so i got a choicebox "categoryDrop" that when the value of the choicebox change i want to trigger this event that then takes the value of the choicebox and compare to an object "Folder" categorylist wich is an attribute it has.
这是我的代码
@FXML
private void folderByCategory(ActionEvent event) {
System.out.println("här1");
TreeItem<DocumentObject<?>> treeRoot = new TreeItem<>(new Folder());
for (Folder folder : logic.getFolderList()) {
if(f.getCategoryList().contains(categoryDrop.valueProperty())){
System.out.println("här2");
TreeItem<DocumentObject<?>> newFolders = new TreeItem<>(folder);
for(FileReference file : folder.getFileList()){
System.out.println(file.getName());
TreeItem<DocumentObject<?>> fileNode = new TreeItem<>(file);
newFolders.getChildren().add(fileNode);
}
treeRoot.getChildren().add(newFolders);
treeRoot.setExpanded(true);
}
treeNav.setRoot(treeRoot);
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我查看scenebuilder时,我没有看到任何实现该方法的好方法,因此它会在它发生变化时触发。有谁知道更好的方法来做到这一点?我应该改用听众吗?
ChoiceBox有一个onAction属性,因此在 FXML 中,您可以简单地将此控制器方法分配给此属性:
<ChoiceBox fx:id="categoryDrop" onAction="#folderByCategory" />
Run Code Online (Sandbox Code Playgroud)
不幸的是,当前版本的 Scene Builder 不支持此属性,因此您无法直接从 Scene Builder 设置此属性。有一个当前问题已为此提交。
一些解决方法是:
onAction属性,如上。ComboBox代替 a ChoiceBox。功能是相似的(虽然不完全相同)并且 aComboBox可能会做你需要的。场景生成器不支持onAction的属性ComboBox。initialize()而是在控制器的方法中注册处理程序。所有你需要的是
@FXML
private ChoiceBox<...> categoryDrop ;
public void initialize() {
categoryDrop.setOnAction(this::folderByCategory);
// existing code ...
}
@FXML
private void folderByCategory(ActionEvent event) {
// existing code...
}
Run Code Online (Sandbox Code Playgroud)