Question

I have the following in my FXMLController:

@FXML
TreeTableView<FileModel> treeTblViewFiles;
//...
@Override
public void initialize(URL url, ResourceBundle rb) {
//...
     final ObservableList<Song> data = FXCollections.observableArrayList(
            new Song("Song#1", "/home/pm/songs/song1.mp3","12MB"),
            new Song("Song#2", "/home/pm/songs/song2.mp3","12MB"),
            new Song("Song#3", "/home/pm/songs/song3.mp3","12MB"),
            new Song("Song#4", "/home/pm/songs/song4.mp3","12MB"),
            new Song("Song#5", "/home/pm/songs/song5.mp3","12MB"),
            new Song("Song#6", "/home/pm/songs/song6.mp3","12MB"),
            new Song("Song#7", "/home/pm/songs/song7.mp3","12MB"),
            new Song("Song#8", "/home/pm/songs/song8.mp3","12MB"),
            new Song("Song#9", "/home/pm/songs/song9.mp3","12MB"),
            new Song("Song#10", "/home/pm/songs/song10.mp3","12MB")
    );
     treeTblViewFiles.setRowFactory(new Callback<TreeTableView<FileModel>, TreeTableRow<FileModel>>(){

        @Override
        public TreeTableRow<FileModel> call(TreeTableView<FileModel> treeTableView) {
            final TreeTableRow<FileModel> row = new TreeTableRow<>();
            final ContextMenu rowMenu = new ContextMenu();
            MenuItem removeItem = new MenuItem("Remove");
            removeItem.setOnAction(new EventHandler<ActionEvent>(){

                @Override
                public void handle(ActionEvent t) {                        
                    data.remove(row.getItem());
                    treeTblViewFiles.getSelectionModel().clearSelection();
                    System.out.println("Context Menu -> ActionEvent");
                }

            });
            rowMenu.getItems().add(removeItem);
            row.contextMenuProperty().bind(Bindings.when(Bindings.isNotNull(row.itemProperty()))
            .then(rowMenu)
            .otherwise((ContextMenu)null));
            return row;
        }

    });
//...
}

Song is a class that inherits from FileModel. Basically what I do is create my custom row factory where I delete the selected item, but nothing happens. No item is deleted from treeTableView control, although it is removed from the ObservableList.

What am I missing or misunderstood? Thanks in advance.

Was it helpful?

Solution

I haven't worked with TreeTableView, so this is a bit of a shot in the dark: but TreeTableViews (and TreeViews) aren't wired to the data quite as cleanly as TableViews. Each data item is wrapped in a TreeItem to give it the hierarchical structure. So I think you need something like

            @Override
            public void handle(ActionEvent t) {  

                data.remove(row.getItem());
                TreeItem<FileModel> treeItem = row.getTreeItem();
                // may need to check treeItem.getParent() is not null:
                treeItem.getParent().getChildren().remove(treeItem);
                treeTblViewFiles.getSelectionModel().clearSelection();
                System.out.println("Context Menu -> ActionEvent");
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top