jeudi 19 mars 2015

JavaFx TreeView with CheckBox and ContextMenu

in the tutorial http://ift.tt/10hbEuA it explain how to create a TreeView with ContextMenu or CheckBox.


but is it possible to have both of them?


when I first copy-paste the code, I learned that I can have only one setCellFactory since they overwrite each other.



// the following two setCellFactory are copied from the tutorial

// this create TreeCell with ContextMenu
treeView.setCellFactory(new Callback<TreeView<String>,TreeCell<String>>(){
@Override
public TreeCell<String> call(TreeView<String> p) {
return new TextFieldTreeCellImpl();
//the class TextFieldTreeCellImp is a TreeCell with ContextMenu
}
});

// this create TreeCell with CheckBox
tree.setCellFactory(CheckBoxTreeCell.<String>forTreeView());


then i tried substituting TreeCell with CheckBoxTreeCell



//class TextFieldTreeCellImpl extends TreeCell<String> {
class TextFieldTreeCellImpl extends CheckBoxTreeCell<String> {
...
//TreeItem newTag = new TreeItem<String>("New tag");
CheckBoxTreeItem newTag = new CheckBoxTreeItem<String>("New tag");


but the checkbox didn't appear. it's still a normal treeview.





Aucun commentaire:

Enregistrer un commentaire