Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 1000
Josejacob99 Avatar asked Jun 30 '13 09:06

Josejacob99


People also ask

How to get selected Object from TableView JavaFX?

How do I get the selected item from a TableView in JavaFX? ObservableList selectedItems = taview. getSelectionModel(). getSelectedItems();

How to show Data in TableView in JavaFX?

The JavaFX TableView enables you to sort the rows in the TableView. There are two ways rows can be sorted. The first way is for the user to click on the TableColumn header cell (where the column title is displayed). This will sort the rows in the TableView after the values of that column.

How to select multiple rows in TableView in JavaFX?

I would add a multi-select button like android. Click the button the subsequent selections get added (or maybe removed after another click) to a list of selections.

How do I filter a TableView?

We can filter TableView content in two main ways – manually, or by using the FilteredList class JavaFX provides. In either case, we can update our search criteria by placing a ChangeListener on the search box TextField. This way, each time the user changes their search, the TableView is updated automatically.


1 Answers

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.

like image 89
Branislav Lazic Avatar answered Sep 25 '22 05:09

Branislav Lazic