JavaFX TreeView adding item to the treeview results the same behavior of the two cells -


i willing parse xml schema definition , treeview parsed file , adding , removing items treeview before generating xml .

my issue when want add treeitem selected 1 -duplicating it- results in misbehavior of 2 treeitems : selected 1 , new one.

for example expanding 1 treeitem results in expanding other .

schematreecell.java

public class schematreecell extends treecell<schemaelement> { gridpane grid = new gridpane();  private jfxbutton bt ; private contextmenu detailsmenu ; private treeitem<schemaelement> newitem; private menuitem showitem ; private menuitem addmenuitem; private menuitem deleteitem; public schematreecell() {     showitem = new menuitem("show details");     addmenuitem = new menuitem("add element");     deleteitem = new menuitem("remove element");     detailsmenu = new contextmenu();     detailsmenu.getitems().add(showitem);     detailsmenu.getitems().add(addmenuitem);     detailsmenu.getitems().add(deleteitem);     bt = new jfxbutton("commit");     showaction();     addaction();     deleteaction(); }   protected void addaction() {     addmenuitem.setonaction(new eventhandler<actionevent>() {         public void handle(actionevent t) {             string max = getitem().getmaxoccurs().equals("unbounded")? "100" : getitem().getmaxoccurs();             if (numberoccurence(gettreeitem())>=integer.parseint(max)) {                 alert alert = new alert(alerttype.warning);                 alert.setheadertext("be careful");                 alert.setcontenttext("verify max occurence of item");                 alert.show();             }             else {                 newitem = new treeitem<>();                 newitem =  gettreeview().getselectionmodel().getselecteditem();                 gettreeitem().getparent().getchildren().add(newitem);                 addmenuitem.setonaction(null);                       t.consume();              }          }      }); } @override protected void updateitem(schemaelement item, boolean empty) {      super.updateitem(item, empty);     if (isempty()) {         setgraphic(null);         settext(null);     }else {     settext(item == null ? ""             : item.getvalue() == null ? "<" + item.getname() + "/>"                     : "<" + item.getname() + ">" + item.getvalue() + "<" + item.     setcontextmenu(detailsmenu);     } } 

this how called cellfactory

getschemaview().setcellfactory(new callback<treeview<schemaelement>, treecell<schemaelement>>() {         @override         public treecell<schemaelement> call(treeview<schemaelement> list) {             return new schematreecell();         }     }); 

you're adding exact same treeitem selected tree. treeitem has expanded state (and other state not want replicated), if expand tree item, both representations expand.

instead, create new tree item same value, , add tree:

addmenuitem.setonaction(new eventhandler<actionevent>() {     public void handle(actionevent t) {         string max = getitem().getmaxoccurs().equals("unbounded")? "100" : getitem().getmaxoccurs();         if (numberoccurence(gettreeitem())>=integer.parseint(max)) {             alert alert = new alert(alerttype.warning);             alert.setheadertext("be careful");             alert.setcontenttext("verify max occurence of item");             alert.show();         }         else {             newitem = createdeepcopy(gettreeview().getselectionmodel().getselecteditem());             gettreeitem().getparent().getchildren().add(newitem);             addmenuitem.setonaction(null);                   t.consume();          }      }  });  // ...  private treeitem<schemaelement> createdeepcopy(treeitem<schemaelement> original) {     treeitem<schemaelement> copy = new treeitem<schemaelement>(original.getvalue());     (treeitem<schemaelement> child : original.getchildren()) {         copy.getchildren().add(createdeepcopy(child));     }     return copy ; } 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -