如何从 java SWT 中的树中删除 TreeItem?

How to remove a TreeItem from a Tree in java SWT?

我正在使用 java SWT 进行应用程序开发。

工具中有一个带有一些子节点的 SWT 树,我想删除其中的一些。我尝试使用 remove() 和 removeAll() 方法,但它并没有完全删除该项目。它为 TreeItem 留下一个 space。

根据 this post,您应该在要完全删除的树项上使用 item.dispose()

TreeItemWidget 的后代,其中包含 dispose() 方法。根据文档:

Disposes of the operating system resources associated with the receiver and all its descendants. After this method has been invoked, the receiver and all descendants will answer true when sent the message isDisposed(). Any internal connections between the widgets in the tree will have been removed to facilitate garbage collection. This method does nothing if the widget is already disposed.

所以 treeItem.dispose() 会起作用。