如何在javaFX中的树视图上使用上下文菜单?

时间:2017-04-21 11:44:41

标签: java javafx treeview contextmenu

我想在TreeView项目上有一个上下文菜单。 我期望事件处理程序的ActionEvent为我提供有关单击哪个TreeView项的信息,但我发现getSource和getTarget返回一个菜单项。如何找出单击了哪个TreeView项?我可以在单独的标签中有多个treevIews。

    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");
        }
    });

称之为:

    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);       
        }
    }

我在这里附上菜单:

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);
    }
}

称之为:

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

我现在尝试使用cellfactory,但我不明白如何使用p参数来查找单元格值。我的代码是:

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 个答案:

答案 0 :(得分:1)

您必须为每个单元格创建不同的上下文菜单:

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 ;
}