我如何清除 JAVA FX 中 tableview 中每一行中单元格数据的所有内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32176782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i clear the all contents of the cell data in every row in my tableview in JAVA FX
提问by Fubby Jim-George Fubarata
I am finding hard to clear the table contents in my table. I have tried this code.
我发现很难清除表格中的表格内容。我试过这个代码。
for ( int i = 0; i<resultTable.getItems().size(); i++) {
resultTable.getItems().clear();
}
To clarify my question, the problem I am having is that i want to delete the values in my table. This was the first code i used;
为了澄清我的问题,我遇到的问题是我想删除表中的值。这是我使用的第一个代码;
public void removeRow(){
allFiles = table.getItems(); fileSelected = table.getSelectionModel().getSelectedItems();
fileSelected.forEach(allFiles :: remove);
}
But it only removes a particular selected row. I want to clear all the rows and leave the table empty at once, without having to select any row. I tried to use this code;
但它只删除特定的选定行。我想清除所有行并立即将表留空,而不必选择任何行。我尝试使用此代码;
public void removeAllRows(){
for ( int i = 0; i<resultTable.getItems().size(); i++) {
resultTable.getItems().clear();
}
}
but it does not clear all the rows in the table
但它不会清除表中的所有行
My plan is to use this method as an action for a button. e.g
我的计划是将此方法用作按钮的操作。例如
Button btn = new ("Clear Table");
btn..setOnAction(e->{removeAllRows();});
When this button is clicked, it should delete all rows in the table at once.
单击此按钮时,应立即删除表中的所有行。
回答by Khush
Try using the following: Name "tableView" to whatever your FXMLtable
is called
尝试使用以下内容:将“tableView”命名为您FXMLtable
所称的任何名称
for ( int i = 0; i<tableView.getItems().size(); i++) {
tableView.getItems().clear();
}
回答by Kevin Hernandez
tableView.getItems().clear()
tableView.getItems().clear()
Will do the trick
会做的伎俩