Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i clear the all contents of the cell data in every row in my tableview in JAVA FX

Tags:

java

javafx

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.

like image 914
Fubby Jim-George Fubarata Avatar asked Aug 24 '15 07:08

Fubby Jim-George Fubarata


People also ask

How to clear a TableView in Java?

tableView. getItems(). clear() clears all TableView data. No need to loop it.

How to set data in TableView in JavaFX?

TableView is a component that is used to create a table populate it, and remove items from it. You can create a table view by instantiating thejavafx. scene. control.

How to select a row in TableView JavaFX?

To select a row with a specific index you can use the select(int) method. Here is an example of selecting a single row with a given index in a JavaFX TableView: selectionModel. select(1);


2 Answers

tableView.getItems().clear()

Will do the trick

like image 163
Kevin Hernandez Avatar answered Oct 19 '22 19:10

Kevin Hernandez


Try using the following: Name "tableView" to whatever your FXMLtable is called

for ( int i = 0; i<tableView.getItems().size(); i++) {
    tableView.getItems().clear();
}
like image 30
Khush Avatar answered Oct 19 '22 20:10

Khush