How to delete row from table column javafx

后端 未结 2 1665
醉话见心
醉话见心 2021-02-10 15:37

These are my table columns Course and Description. If one clicks on a row (the row becomes \'active\'/highlighted), and they

相关标签:
2条回答
  • 2021-02-10 15:47

    If someone want to remove multiple rows at once, there is similar solution to accepted:

    First we need to change SelectionMethod in our table to allow multiple selection:

    table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
    

    After this, we need to set action with such code for button:

    ObservableList<SomeField> selectedRows = table.getSelectionModel().getSelectedItems();
    // we don't want to iterate on same collection on with we remove items
    ArrayList<SomeField> rows = new ArrayList<>(selectedRows);
    rows.forEach(row -> table.getItems().remove(row));
    

    We could call removeAll method instead of remove(also without creating new collection), but such solution will remove not only selected items, but also their duplicates if they exists and were not selected. If you don't allow duplicates in table, you can simply call removeAll with selectedRows as parameter.

    0 讨论(0)
  • 2021-02-10 15:49

    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);
    });
    
    0 讨论(0)
提交回复
热议问题