2
Greetings to all. This is my first post here.
I am developing a Java FX project that consists of a form with a Treeview whose items are obtained from an Arraylist, previously completed through a database query.
This table represents an item hierarchy where each item has an id, name and parent_id (id of another table item, taken as the parent of the item in question).
To mount tree I am using a foreach to select the item in the tree whose id corresponds to the parent_id (c.getParent) of each item to be added :
for (Conta c : list){
try {
tree.getSelectionModel().select(c.getParent());
MyTreeItem item = (MyTreeItem)tree.getSelectionModel().getSelectedItem();
item.setExpanded(true);
MyTreeItem conta = new MyTreeItem (c.getNome(),c.getId());
item.getChildren().add(conta);
} catch (Exception e) {
System.out.println(e);
}
}
This method worked as I expected:
When I first ran the project the table already had the records and the tree was mounted correctly. Then I implemented methods to add children to any node selected by the user. The problem is that I realized that although the method is saving the new item correctly in the BD (see image below), if I update the tree, or close and open the program again, the tree will display the newly added item as the child of an item other than the one chosen and that is pointed out by the parente_id field as parent.
Note that the item "Current account 6666" (parent_id = 15) should be the child of the "Bank of Boston" account (id = 15)
Then I created a custom Treeitem with the id and name parameters for, when mounting the tree try to organize the hierarchy using as a criterion the id that the element represented in the tree has in the database. It was then that I realized that the problem lies in this line: tree.getSelectionModel().select(c.getParent());
because it selects the item by index in Treeview, only this index does not match the id of the element in the database.
My question is how do I make that line of code tree.getSelectionModel().select(int index);
select an item in a Treeview not by the index parameter (item position in Treeview(?)), but using as parameter the identifier (int id) that I created in a class derived from Treeitem with this attribute.
Thanks to everyone from now on.
I did a new project and added your code to replace the one I was using. I ran the application and it apparently worked except because the tree items are displaying the memory addresses of the objects(guess this is it). How do I display only the account name on the tree? I tried something like "no.getChildren(). setAll(sons);" or "treeItems.add(newItem.getValue();" but I couldn’t... Any hint?
– Samuel Santos
I forgot to mention this detail. Whenever we use objects we have to say how it will be represented by overwriting the toString method. I will change the answer.
– Gustavo Fragoso
Very good! I followed your guidance and overwritten toString, then implemented the necessary SQL methods to insert the new items from the tree into the BD and added to the logic you used for the add button. Following the same reasoning I also made a button/method to remove the items from the tree and the bank. It worked perfectly.Thank you very much.
– Samuel Santos