Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

java - Getting selected item from a JavaFX TableView

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

I am currently using

ObservableList selectedItems = taview.getSelectionModel().getSelectedItems();

but that does not return me the one selected item in the selection model.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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<Person> taview;

or

TableView<Person> taview = new TableView<>();

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...