我的 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 控件中删除任何项目。
我错过了什么或误解了什么?提前致谢。