TableView和ListView的删除方式如何工作? [英] How TableView and ListView's remove works?
问题描述
正如我所见,我们必须覆盖等于
和 hashCode
方法从java Collection中删除Custom类的对象 java.util.List
或类似的其他人。
As I saw we have to override equals
and hashCode
methods to remove Custom class's object from the java Collection's java.util.List
or similar others.
但我想知道如何 TableView
和 ListView
的删除
有效吗?最近我成功从 TableView
的中删除了一个自定义类
(在查看(Person)
的对象ObservableList ObservableList
的删除
方法后,我意识到它正在继承从
) java.util.List中删除(对象)
方法
But I want to know how the TableView
and ListView
's remove
works? recently I successfully removed an custom class (Person)
's object from the TableView
's ObservableList
( after viewing the ObservableList
's remove
method I realized it is inheriting the remove(Object)
method from the java.util.List
)
然后如何通过调用删除TableView的行。 table.getItems()。remove(row.getItem())
row包含Person类的对象数据,Person类未覆盖 equals(object) ;
和 hashCode();
方法
Then how it could be possible to remove the TableView's row by calling. table.getItems().remove(row.getItem())
row contain Person class's object data and Person class didn't override equals(object);
and hashCode();
methods
以下是关于此混淆的完整代码。
Here is the complete code about this confusion.
public class RowMenu implements Callback<TableView<Person>, TableRow<Person>> {
@Override
public TableRow<Person> call(TableView<Person> table){
final TableRow row = new TableRow();
final ContextMenu contextMenu = new ContextMenu();
final MenuItem removeMenuItem = new MenuItem("Remove");
removeMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println(row.getItem());
System.out.println(table.getItems().remove(row.getItem()));
}
});
contextMenu.getItems().addAll(removeMenuItem);
row.setContextMenu(contextMenu);
row.contextMenuProperty().bind(
Bindings.when(row.emptyProperty())
.then((ContextMenu)null)
.otherwise(contextMenu)
);
return row;
}
}
推荐答案
默认比较只使用对象标识;即如果你不覆盖等于(...)
那么 object.equals(anotherObject)
相当于 object == anotherObject
。
The default comparison just uses object identity; i.e. if you don't override equals(...)
then object.equals(anotherObject)
is equivalent to object == anotherObject
.
在这种情况下, list.remove(object)
将删除行
来自 list
如果引用 object
与列表所持的引用相同。
In that case, list.remove(object)
will remove a row
from list
if the reference object
is identically equal to a reference held by the list.
在这种情况下:
table.getItems().remove(row.getItem())
显然可行。 row.getItem()
返回表行显示的项目,该表项与表视图的数据备份列表中保存的对象完全相同。 (表视图机制将表视图的支持列表中的引用传递给表行的 updateItem(...)
方法。)
will clearly work. row.getItem()
returns the item displayed by the table row, which is the very same object that is held in the table view's backing list of data. (The table view mechanism passes a reference from the table view's backing list to the table row's updateItem(...)
method.)
如果你想为 remove(...)
提供等于<的参数,你只需要担心重写等于/ code>到要删除的对象,但可能不是对内存中实际相同对象的引用。
You only need to worry about overriding equals if you want to supply an argument to remove(...)
that is equal
to the object you want to remove, but might not be a reference to the actual same object in memory.
这篇关于TableView和ListView的删除方式如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!