Is this possible that i can add context menu at TreeItem rather then TreeView in JavaFX


i am new to JavaFX. I made the custom TreeCell using this reference:-

It perfectly worked for me. But my case is that i do not want to apply this context menu to child nodes. At start there is only single TreeItems and at later stage these TreeItems have child so the try to check isLeaf() method does not make a sense because at starting the TreeItems are at leaf level but later they become parent.

So any idea how can i achieve this.

Thanks a lot.

At initial stage of my project there is tree having three childs(mail service providers) as follows:-


After i connect these providers then they have this structure:-


Now i want context menu only on Gmail,Yahoo and rediff and not on the inbox or sent item.

I know the setContextmenu() method but this is available for the TreeView and not for the TreeItem.

12/19/2013 8:26:09 PM

Accepted Answer

I would make your tree items a descendant class of TreeItem like public class ProviderTreeItem extends TreeItem and a different descendant with the boxes like BoxTreeItem.

That way you won't test for isLeaf but you can test the TreeItem type.

if (thisTreeItem.getClass() == ProviderTreeItem.class)
else thisTreeItem.setContextmenu(boxContextMenu)

It would be easier to just get the context menu from the subclassed TreeItem.

You don't really need to subclass TreeItem, you could just use the name or something like the userData object to distinguish between node types. If you subclass then you can add different methods and properties to the different node types.

For example the boxes will need a data structure to hold emails but the the providers don't need this. The providers need a web address and password etc., but the boxes don't need that.

Here's the updated example.

import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Callback;

public class TVexample extends Application {
public abstract class AbstractTreeItem extends TreeItem{
    public abstract ContextMenu getMenu();

public class ProviderTreeItem extends AbstractTreeItem{
    // make class vars here like psswd
    public ProviderTreeItem(String name) {

    public ContextMenu getMenu(){
        MenuItem addInbox = new MenuItem("add inbox");
        addInbox.setOnAction(new EventHandler() {
            public void handle(Event t) {
                BoxTreeItem newBox = new BoxTreeItem("inbox");
        return new ContextMenu(addInbox);

public class BoxTreeItem extends AbstractTreeItem{
    //private List<String> emails = new LinkedList<>();
    public BoxTreeItem(String name) {

    public ContextMenu getMenu() {
        return new ContextMenu(new MenuItem("test"));

private final class TreeCellImpl extends TreeCell<String> {

    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);

        if (empty) {
        } else {
            setText(getItem() == null ? "" : getItem().toString());
            setContextMenu(((AbstractTreeItem) getTreeItem()).getMenu());

public void start(Stage primaryStage) {
    StackPane sceneRoot = new StackPane();
    TreeItem treeRoot = new TreeItem();
    ProviderTreeItem gm = new ProviderTreeItem("gmail");
    ProviderTreeItem yh = new ProviderTreeItem("yahoo");
    TreeView<String> treeView = new TreeView<String>(treeRoot);
    treeView.setCellFactory(new Callback<TreeView<String>,TreeCell<String>>(){
        public TreeCell<String> call(TreeView<String> p) {
            return new TreeCellImpl();
    Scene scene = new Scene(sceneRoot, 300, 500);



12/20/2013 3:34:47 PM

Here is my favorite utility method for defining cellFactory in TreeView. Firstly, it sets TreeCell conversion to String, secondly, it applies ContextMenu.

public static <T> void setCellFactory(TreeView<T> treeView,
                                      ToStringConverter<T> converter,
                                      ContextMenu contextMenu) {

treeView.setCellFactory(tc -> {

  TreeCell<T> cell = new TreeCell<T>() {

    protected void updateItem(T item, boolean empty) {
      super.updateItem(item, empty);
      if (empty) {
      } else {


      .addListener((obs, wasEmpty, isNowEmpty) -> {
        if (isNowEmpty) {
        } else {

  return cell;



ToStringCoverter is interface

public interface ToStringConverter<T> {
  String toString(T t);

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow