36

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.

randers
  • 5,031
  • 5
  • 37
  • 64
Josejacob99
  • 415
  • 1
  • 4
  • 9

8 Answers8

95

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.

Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85
9
    tableview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
    @Override
    public void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
        //Check whether item is selected and set value of selected item to Label
        if(tableview.getSelectionModel().getSelectedItem() != null) 
        {    
           TableViewSelectionModel selectionModel = tableview.getSelectionModel();
           ObservableList selectedCells = selectionModel.getSelectedCells();
           TablePosition tablePosition = (TablePosition) selectedCells.get(0);
           Object val = tablePosition.getTableColumn().getCellData(newValue);
           System.out.println("Selected Value" + val);
         }
         }
     });

Using this code you can get the selected value from JAVAFX TABLEVIEW Cell.

Thanks..

Java Man
  • 1,854
  • 3
  • 21
  • 43
  • this code worked for me. But it works only when I select some another row. How do I run this code everytime a tablecell is selected even if the same row is selected – viper Dec 16 '15 at 17:41
  • There is any way to get the mouse coordinate inside this listener to show something like a popup menu? – leobelizquierdo Apr 29 '16 at 21:59
3

For single item selection...

Product p = taview.getSelectionModel().getSelectedItem();
System.out.println(p.getName()); 

For multiple item selection...

ArrayList<Product> p = new ArrayList<>(taview.getSelectionModel().getSelectedItems());
for (Product res : p) {     
    System.out.println(res.getName());      
}
Abra
  • 19,142
  • 7
  • 29
  • 41
2

@pranishres

Here is my try for a cell selection change listener (even if row doesn’t change), inspired by @Java Man's code:

tableView.getSelectionModel().setCellSelectionEnabled(true);
ObservableList selectedCells = tableView.getSelectionModel().getSelectedCells();

selectedCells.addListener(new ListChangeListener() {
    @Override
    public void onChanged(Change c) {
        TablePosition tablePosition = (TablePosition) selectedCells.get(0);
        Object val = tablePosition.getTableColumn().getCellData(tablePosition.getRow());
        System.out.println("Selected Value" + val);
    }
});
O. Durand
  • 169
  • 1
  • 8
0

you can get data from tableview like that

For One ItemSelection

`tableview_obj.getSelectionModel().getSelectedItem().yourgetterobjectname`

and Mulltiple Item Selection

tableview_obj.getSelectionModel().getSelectedItems().get(indexNo).yourgetterobjectname

13hola
  • 170
  • 1
  • 12
0

you can use this method maybe it works for you

ObservableList rowList = (ObservableList) tblView.getItems().get(getIndex());

and for the call method will be like that

String email = rowList.get(1).toString();
String contact = rowList.get(2).toString();
String name= rowList.get(0).toString();
Samer
  • 126
  • 1
  • 9
-1
Object row2 = tableEmployees.getSelectionModel().getSelectedItems().get(0);
        String ordre = row2.toString().split(",")[0].substring(1);
        System.out.println("Ordre N " + ordre);

well this is the best solution you can found it ever

Samer
  • 126
  • 1
  • 9
-2

if you have a Bean class named Employee.

Employee employee= tblView.getSelectionModel().getSelectedItem();
txtEmployeeName.setText(employee.getName());
Kamran
  • 669
  • 6
  • 9