Getting selected item from a JavaFX TableView

前端 未结 7 1600
轻奢々
轻奢々 2020-12-15 16:01

How do I get the selected item from a TableView in JavaFX?

I am currently using

ObservableList selectedItems = taview.getSelectionModel(         


        
7条回答
  •  失恋的感觉
    2020-12-15 16:32

    Ok, lets say you have a data model class named Person. This way:

    Person person = taview.getSelectionModel().getSelectedItem();
    System.out.println(person.getName());    
    

    Note that TableView must take a Person as a type argument to avoid casting:

    @FXML
    private TableView taview;
    

    or

    TableView taview = new TableView<>();
    

    when your row is selected, you will return one Person instance. Then do what ever you want with that instance.

提交回复
热议问题