我的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的类。 基本上我所做的是创建我的自定义行工厂,我删除所选项目,但没有任何反应。没有项目从treeTableView控件中删除,尽管它已从ObservableList中删除。
我错过或误解了什么? 提前谢谢。
答案 0 :(得分:1)
我没有使用过TreeTableView
,所以这在黑暗中是一个镜头:但是TreeTableView
s(和TreeView
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");
}