Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete JavaFX table row with delete key

Is there a way to delete selected table row using keyboard delete key?

Is there any example with this implementation?

like image 705
Peter Penzov Avatar asked Dec 20 '22 08:12

Peter Penzov


1 Answers

Sure you can. You only have to register an EventHandler and listen to the specific KeyCode. Following example is for TreeTableView but should be applyable for all TableViews.

treeTableView.setOnKeyPressed( new EventHandler<KeyEvent>()
{
  @Override
  public void handle( final KeyEvent keyEvent )
  {
    final TreeItem<YourObject> selectedItem = treeTableView.getSelectionModel().getSelectedItem();

    if ( selectedItem != null )
    {
      if ( keyEvent.getCode().equals( KeyCode.DELETE ) )
      {
        //Delete or whatever you like:
        presenter.onEntityDeleteAction( selectedItem );
      }

       //... other keyevents
    }
  }
} );
like image 183
crusam Avatar answered Dec 28 '22 08:12

crusam