5

So, I'm trying to delete the highlighted row in TableView in my program.

I've looked at loads of tutorials online, but really can't get my head around this. I have followed the example from CodeMakery, which is in Eclipse, but I'm unable to get it to work on IntelliJ (because of some apparent JDK problems?)

This is the code from CodeMakery:

private void handleDeletePerson() {
    int selectedIndex = personTable.getSelectionModel().getSelectedIndex();
    if (selectedIndex >= 0) {
        personTable.getItems().remove(selectedIndex);
    } else {
        // Nothing selected.
        Alert alert = new Alert(AlertType.WARNING);
        alert.initOwner(mainApp.getPrimaryStage());
        alert.setTitle("No Selection");
        alert.setHeaderText("No Person Selected");
        alert.setContentText("Please select a person in the table.");

        alert.showAndWait();
    }
}

Could you please help me understand how to make selected row get deleted?

Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
april21
  • 115
  • 2
  • 9
  • 1
    The code you posted already answers the question "How do I delete the selected row from a `TableView`?". If this is generating errors then you have problems either with your other code or (as you already suggested) with your configuration. You need to at least show the errors you are getting and, if appropriate, the code that is causing them.\ – James_D Jul 12 '15 at 13:04
  • @James_D, IntelliJ doesn't recognise the Alert statement. Is there some library I should import? – april21 Jul 12 '15 at 14:09
  • 2
    Make sure you are using at least Java 1.8.0_40, which is when Alert was introduced. Or, just remove the code that uses `Alert` and use a different solution for that case. – James_D Jul 12 '15 at 14:50

1 Answers1

-1

just add at the beginning of this method personTable.setEditable(true). Should work now.

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Andrew Petryk
  • 229
  • 2
  • 4