0

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.

Mike Spike
  • 389
  • 8
  • 20
  • Something looks weird with your model. Your TreeTableRow is a TreeTableRow, so row.getItem() is going to return a FileModel object. data is an ObservableList and consists of Song objects. So it doesn't seem like row.getItem() can ever be a member of data, and so data.remove(row.getItem()) should basically be a no-op. – James_D Apr 03 '14 at 02:26
  • Song is a FileModel. It is being deleted from data but not from treeTable – Mike Spike Apr 03 '14 at 06:02

1 Answers1

1

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");
            }
James_D
  • 201,275
  • 16
  • 291
  • 322
  • it was a shiny shot :) ..worked. I thought that mechanism with observableArrayList would do the thing, but apparantly I had to implement my own changeListener on that model – Mike Spike Apr 06 '14 at 11:50