Greetings to all. This is my first post here.
I'm developing a Java FX project that consists of a form with a TreeView whose items are obtained from an ArrayList, previously populated through a query to the database.
Thistablerepresentsahierarchyofitemswhereeachitemhasanid,name,andaparent_id(idofanothertableitem,takenastheparentoftheiteminquestion).
TomounttreeI'musingaforeachtoselecttheiteminthetreewhoseidcorrespondstotheparent_id(c.getParent)ofeachitemtobeadded:
for(Contac:list){try{tree.getSelectionModel().select(c.getParent());MyTreeItemitem=(MyTreeItem)tree.getSelectionModel().getSelectedItem();item.setExpanded(true);MyTreeItemconta=newMyTreeItem(c.getNome(),c.getId());item.getChildren().add(conta);}catch(Exceptione){System.out.println(e);}}
ThismethodworkedasIexpected:
WhenIexecutedtheprojectthefirsttimethetablealreadyhadtherecordsandthetreewasmountedcorrectly.ThenIimplementedmethodstoaddchildrentoanynodeselectedbytheuser.TheproblemisthatIrealizedthatalthoughthemethodissavingthenewitemcorrectlyintheDB(seeimagebelow),ifIupdatethetree,orcloseandopentheprogramagain,thetreewillshowthenewlyaddeditemasthechildofaitemotherthanwhathadbeenchosenandwhichispointedtobytheparente_idfieldasparent.
Notethattheitem"Current Account 6666" (parent_id = 15) should be the child of the "Bank of Boston" account (id = 15)
So I created a custom TreeItem with the parameters id and name to at the time of mounting the tree trying 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 is in this line: tree.getSelectionModel().select(c.getParent());
because it selects the item by index in Treeview, except that this index does not correspond to the id of the element in the database.
My question is how do I make the line of code tree.getSelectionModel().select(int index);
select an item in a TreeView not by the index parameter (position of the item in the TreeView (?)), but using the identifier ) that I created in a TreeItem-derived class with this attribute.
Thanks to all of you right away.