0

I created a treeView using fxml, then i populate the data in the controller (in the Initialize()). I added a textfield with a listner, when the user type something on it i should search for the item correspond on a treeview and display it only. I am not sure how to search or iterate on a treeview.. here is my code:

@FXML private TreeView myTreeView;

@FXML public TextField searchTextField; 

public Preferences myPref = Preferences.userNodeForPackage(this.getClass());

   @Override

   public void initialize(URL url, ResourceBundle rb) {

        //root
        TreeRoot = new TreeItem("TreeItems");

        //1st node
        material = new TreeItem("Material");
        //add children to 1st node
        TreeItem<String> cardsType = new TreeItem("CardsType");
        TreeItem<String> memory = new TreeItem("HostMemory" );
        nodes.getChildren().addAll(cardsType,memory);

        //2nd node
        applicationsType = new TreeItem("Application Type");
        //add children to 2nd node
        TreeItem<String> streamType = new TreeItem("streamTypes");
        TreeItem<String> direction = new TreeItem("direction");
        appsType.getChildren().addAll(streamType,direction);

        //add other nodes here...

        //set root and nodes
        itemTreeView.setRoot(TreeRoot);
        TreeRoot.getChildren().addAll(material,,applicationsType);
        myTreeView.setShowRoot(false);

        //save preferences 
        TreeRoot.setExpanded(myPref.getBoolean("TreeItems", true));
        material.setExpanded(myPref.getBoolean("material", true));
        applicationsType.setExpanded(myPref.getBoolean("Application Type", true));

         SearchTextField.setPromptText("Search");
         SearchTextField.textProperty().addListener(new ChangeListener<String>()  {
        @Override
            public void changed(ObservableValue<? extends String> observable,
                            String oldValue, String newValue) {
                //we could use directly the newValue
                String value = m_SearchTextField.getText();
                // loop and iterate on the treeView , find items in nodes and compare with the string typed 
                //on the textfield
            }
        });


        }
James_D
  • 201,275
  • 16
  • 291
  • 322
coeur
  • 41
  • 2
  • 10
  • I found an answer here: http://stackoverflow.com/questions/11084027/csearch-from-treeview, but the code is in c#, i never used C#, So i'm not sure i could really use it. I'll be grateful for any help, thanx in advance. – coeur Jul 06 '15 at 15:46
  • possible duplicate of [Iterate TreeView nodes](http://stackoverflow.com/questions/28342309/iterate-treeview-nodes) – James_D Jul 06 '15 at 16:40
  • @ James_D i saw that answer but i didn't really understand it , that's why i created a new question – coeur Jul 06 '15 at 16:43
  • Your question is essentially identical, so any answer to this is likely to be basically the same as an answer to that question. It's up to you to read the code that's there and figure out what it does. The answer contains a complete example, so you can copy the code, execute it, edit it and experiment, etc... – James_D Jul 06 '15 at 16:47

0 Answers0