How to delete row from table column javafx

Pim picture Pim · Jan 18, 2016 · Viewed 22k times · Source

enter image description here

These are my table columns Course and Description. If one clicks on a row (the row becomes 'active'/highlighted), and they press the Delete button it should remove that row, how do I do this?

The code for my Course column: (and what event listener do I add to my delete button?)

@SuppressWarnings("rawtypes")
TableColumn courseCol = new TableColumn("Course");
courseCol.setMinWidth(300);
courseCol.setCellValueFactory(new PropertyValueFactory<Courses, String>("firstName"));

final Button deleteButton = new Button("Delete");

deleteButton.setOnAction(.....

Answer

James_D picture James_D · Jan 18, 2016

Just remove the selected item from the table view's items list. If you have

TableView<MyDataType> table = new TableView<>();

then you do

deleteButton.setOnAction(e -> {
    MyDataType selectedItem = table.getSelectionModel().getSelectedItem();
    table.getItems().remove(selectedItem);
});