简体   繁体   English

如何在JavaFX TreeView中搜索下一个TreeItem?

[英]How to search JavaFX TreeView for next TreeItem?

I would like to setup some search functionality so that when you type text into a TextField and press enter it finds the first instance of that String in a TreeView. 我想设置一些搜索功能,以便当您在TextField中键入文本并按Enter时,它将在TreeView中找到该字符串的第一个实例。 Then, when you press enter again, it will finds the second instance, pressing it again will find the third, ect... 然后,当再次按Enter键时,它将找到第二个实例,再次按它将会找到第三个实例,等等。

It doesn't seem too hard to be able to search a TreeView and find the first TreeItem containing text, but I am unsure how to then keep searching down the TreeView for other TreeItems containing said text. 能够搜索TreeView并找到第一个包含文本的TreeItem似乎并不难,但是我不确定如何继续在TreeView上搜索包含该文本的其他TreeItem。 I imagine to search through all the Folders/Documents inside a TreeView would require some sort of recursive function, but am unsure. 我想在TreeView中搜索所有文件夹/文档将需要某种递归功能,但是不确定。 I would therefore like to ask if anyone might be able to provide an example or some tips of how to create this search functionality. 因此,我想问一问是否有人能够提供一个示例或一些有关如何创建此搜索功能的提示。 Any help would be appreciated :) 任何帮助,将不胜感激 :)

Drew 德鲁

You could a recurive function to search through the tree. 您可以使用递归函数来搜索树。

In the below code I have maintained 1 instance variable it holds the number of times searchBtn has been clicked, although this solves the question, its not quite optimal, when the user clicks the search button again although it gets the next match, it traverses the tree from the beginning (not from the previous match), you could use this as a solution for now, I will try to come up with a more optimal solution in the mean time 在下面的代码中,我维护了1个实例变量,该变量保存了单击searchBtn的次数,尽管这解决了这个问题,但并不是十分理想,当用户再次单击搜索按钮(尽管它获得下一个匹配项)时,它将遍历从一开始(不是从上一个比赛开始)开始,您现在可以将其用作解决方案,与此同时,我将尝试提出一个更优化的解决方案

 public class TreeViewSample extends Application {

private int count=0;

private TreeView<String> tree;
public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("Tree View Sample");        


    TextField txtField=new TextField();
    Button searchBtn=new Button("Search");

    txtField.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent arg0) {
            count=0;
        }
    });
    TreeItem<String> rootItem = new TreeItem<String> ("Root");
    rootItem.setExpanded(true);

    TreeItem<String> item1 = new TreeItem<String> ("Child 1");
    TreeItem<String> item12 = new TreeItem<String> ("Child 12");
    item1.getChildren().add(item12);
    TreeItem<String> item2 = new TreeItem<String> ("Child 2");
    TreeItem<String> item21 = new TreeItem<String> ("Child 21");
    item2.getChildren().add(item21);
    TreeItem<String> item211 = new TreeItem<String> ("Child 12");
    item21.getChildren().add(item211);
    TreeItem<String> item3 = new TreeItem<String> ("Child 3");
    TreeItem<String> item31 = new TreeItem<String> ("Child 12");
    item3.getChildren().add(item31);
    rootItem.getChildren().addAll(item1,item2,item3);
    searchBtn.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent arg0) {
            count++;
            handleSearch(rootItem,txtField.getText());

        }
    });
    this.tree = new TreeView<String> (rootItem);    

    VBox root = new VBox();
    root.getChildren().addAll(tree,txtField,searchBtn);
    primaryStage.setScene(new Scene(root, 300, 250));
    primaryStage.show();
}

protected int handleSearch(TreeItem<String> rootItem, String text) {
    int count=0;
    if(rootItem!=null&&rootItem.getValue().equals(text)){
        tree.getSelectionModel().select(rootItem);
        return 1;
    }
    if(rootItem!=null&&!rootItem.getChildren().isEmpty()){
        for(TreeItem<String> treeItem: rootItem.getChildren()){
            count+=handleSearch(treeItem, text);
            if(this.count==count){
                break;
            }
        }
    }
    return count;
}
}

Note that you could also use a tree iterator and iterate it in a loop, if you don't want to use recursions 请注意,如果您不想使用递归,还可以使用树迭代器并在循环中对其进行迭代

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

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