0

我的 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 是一个继承自 FileModel 的类。基本上我所做的是创建我的自定义行工厂,在其中删除所选项目,但没有任何反应。尽管它已从 ObservableList 中删除,但不会从 treeTableView 控件中删除任何项目。

我错过了什么或误解了什么?提前致谢。

4

1 回答 1

1

我没有使用过TreeTableView,所以这有点像在黑暗中拍摄:但是TreeTableViews(和TreeViews)并没有像 s 那样干净地连接到数据TableView。每个数据项都包装在 TreeItem 中,以赋予其层次结构。所以我认为你需要类似的东西

            @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");
            }
于 2014-04-03T13:06:40.190 回答