Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete row from table column javafx

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(.....
like image 798
Pim Avatar asked Jan 18 '16 14:01

Pim


1 Answers

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);
});
like image 116
James_D Avatar answered Sep 19 '22 11:09

James_D