How to make a context menu work on a treeview in javaFX?

孤人 提交于 2021-02-04 21:50:32

问题


I want to have a context menu on a TreeView item. I am expecting that the ActionEvent of the event handler gives me information about which TreeView item is clicked, but I just find that getSource and getTarget return a Menu Item. How can I find out which TreeView Item has been clicked? I can have multiple treevIews in separate Tabs.

    FileContextMenu cm = new FileContextMenu(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent e) {
            System.out.println("Open File");
            //MenuItem mi = (MenuItem)e.getSource();
            EventTarget et = e.getTarget();
            //File editorFile = new File(mi.getId());
            System.out.println(et);
            //mainWindowController.openEditor(editorFile);
        }
    }, new EventHandler<ActionEvent>() {
        public void handle(ActionEvent e) {
            System.out.println("Create Project From Template");
        }
    });

which calls this:

    public class FileContextMenu extends ContextMenu
    {

            public FileContextMenu(EventHandler<ActionEvent> ehOpenFile, 

EventHandler<ActionEvent> ehProjectFromTemplate)
        {
            MenuItem item1 = new MenuItem("Open File");
            item1.setOnAction(ehOpenFile);
            MenuItem item2 = new MenuItem("Create Project From Template");
            item2.setOnAction(ehProjectFromTemplate);
            this.getItems().addAll(item1, item2);       
        }
    }

I am attaching the menu here:

private void addFilesTab(FXMLLoader loader, String sRoot, ContextMenu cm) throws IOException
{
    AnchorPane fileView = loader.load();
    FileViewController fileViewController = loader.getController();

    FileShort fsRoot = new FileShort(sRoot);
    if(fsRoot.exists()) {
        fileViewController.setRootFolder(fsRoot);
        fileViewController.setContextMenu(cm);
        ObservableList<Tab> tabs = navigationTabPane.getTabs();
        tabs.add(new Tab(sRoot));
        // Connect the FileView with last tab of the Navigation TabPane.
        tabs.get(tabs.size()-1).setContent(fileView);
    }
}

which calls this:

public void setContextMenu(ContextMenu cm) 
{
    fileTreeView.setContextMenu(cm);
}

I now try to use a cellfactory, but I don't understand how to use the p parameter to find a cells value . My code for this is:

this.fileTreeView.setCellFactory(new Callback<TreeView<FileShort>,TreeCell<FileShort>>(){
    @Override
    public TreeCell<FileShort> call(TreeView<FileShort> p) {
        TreeCell<FileShort> cell = new TreeCell<FileShort>();
        cell.setContextMenu(cm);
        return cell;
    }
});

回答1:


You have to create a different context menu for each cell:

this.fileTreeView.setCellFactory(new Callback<TreeView<FileShort>,TreeCell<FileShort>>(){
    @Override
    public TreeCell<FileShort> call(TreeView<FileShort> p) {
        TreeCell<FileShort> cell = new TreeCell<FileShort>() {
            @Override
            protected void updateItem(FileShort file, boolean empty) {
                super.updateItem(file, empty);
                if (empty) {
                    setText(null);
                } else {
                    // maybe use a more appropriate string for display here
                    // e.g. if you were using a regular java.io.File you would
                    // likely want file.getName()
                    setText(file.toString());
                }
            }
        };
        ContextMenu cm = createContextMenu(cell);
        cell.setContextMenu(cm);
        return cell;
    }
});

private ContextMenu createContextMenu(TreeCell<FileShort> cell) {
    ContextMenu cm = new ContextMenu();
    MenuItem openItem = new MenuItem("Open File");
    openItem.setOnAction(event -> {
        FileShort file = cell.getItem();
        if (file != null) {
            // open the file...
        }
    });
    cm.getItems().add(openItem);
    // other menu items...
    return cm ;
}


来源:https://stackoverflow.com/questions/43541884/how-to-make-a-context-menu-work-on-a-treeview-in-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!