treeview - right click menu option in the tree root node -


i want right click menu option in tree root node(javafx). 1 me on this.

treeitem<string> root = new treeitem<>(""+selecteddirectory); root.setexpanded(true);  locationtreeview.setroot(root);  root.getchildren().addall(     new treeitem<>("item 1"),     new treeitem<>("item 2"),     new treeitem<>("item 3") ); 

you can perform desired behaviour in 2 steps:

  1. defining custom treecell factory on treeview;
  2. attaching context menu on treecell of root tree item.

the following code defines custom treecell factory:

// defines custom tree cell factory tree view tree.setcellfactory(new callback<treeview<string>, treecell<string>>() {      @override     public treecell<string> call(treeview<string> arg0) {         // custom tree cell defines context menu root tree item         return new mytreecell();     } }); 

and, here implementation of custom tree cell attaches context menu root tree item:

class mytreecell extends textfieldtreecell<string> {     private contextmenu rootcontextmenu;      public mytreecell() {         // instantiate root context menu         rootcontextmenu =              contextmenubuilder.create()                 .items(                         menuitembuilder.create()                             .text("menu item")                             .onaction(                                 new eventhandler<actionevent>() {                                     @override                                     public void handle(actionevent arg0) {                                         system.out.println("menu item clicked!");                                                                                }                                 }                             )                             .build()                     )                 .build();     }      @override     public void updateitem(string item, boolean empty) {         super.updateitem(item, empty);          // if item not empty , root...         if (!empty && gettreeitem().getparent() == null) {             setcontextmenu(rootcontextmenu);         }     } } 

the following example ilustrates use of both, cell factory , custom cell, together:

public class treeviewwithcontextmenuonroot extends application {     public static void main(string[] args) {         launch(args);     }      @override     public void start(stage primarystage) {         primarystage.settitle("tree context menu on root");                  treeitem<string> rootitem = new treeitem<string> ("tree root");         rootitem.setexpanded(true);         (int = 1; < 3; i++) {             treeitem<string> item = new treeitem<string> ("item" + i);                         rootitem.getchildren().add(item);         }                 final treeview<string> tree = new treeview<string> ();             tree.setroot(rootitem);          // defines custom tree cell factory tree view         tree.setcellfactory(new callback<treeview<string>, treecell<string>>() {              @override             public treecell<string> call(treeview<string> arg0) {                 // custom tree cell defines context menu root tree item                 return new mytreecell();             }         });          stackpane root = new stackpane();         root.getchildren().add(tree);         primarystage.setscene(new scene(root, 200, 100));         primarystage.show();     }      private static class mytreecell extends textfieldtreecell<string> {         private contextmenu rootcontextmenu;          public mytreecell() {             // instantiate root context menu             rootcontextmenu =                  contextmenubuilder.create()                     .items(                             menuitembuilder.create()                                 .text("menu item")                                 .onaction(                                     new eventhandler<actionevent>() {                                         @override                                         public void handle(actionevent arg0) {                                             system.out.println("menu item clicked!");                                                                                    }                                     }                                 )                                 .build()                         )                     .build();         }          @override         public void updateitem(string item, boolean empty) {             super.updateitem(item, empty);              // if item not empty , root...             if (!empty && gettreeitem().getparent() == null) {                 setcontextmenu(rootcontextmenu);             }         }     } } 

you can take @ treeview tutorial see other uses , examples related javafx control.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -