How to get TableView Row data using an index?

Java

10/08/2017

Hi there. I'll need to get the TableRow Data (in a TableView (JavaFX)) using the index of a specific row. For exemple: there is a TableView with 3 rows and each row with 3 columns. I want to get the 3 columns of row number 2 (index of row 2 is 1). I saw in the WEB that i can get the entire row and convert it to String. Take a look below:
myTableView.setRowFactory (tv ->
{
TableRow<ObservableList> myRow = new TableRow<>();
theRow.setOnMouseClicked (event ->
{
if (event.getClickCount() == 1 && (!myRow.isEmpty()))
{
int myIndex =
myTableView.getSelectionModel().getSelectedIndex();

String myRowData =
myTableView.getItems().get(myIndex).toString();
}
});
return myRow;
});

The code above run ok. I get the Index correctly. But the content of myRowData isn't correct. Can anyone help me?
Lucio

Lucio

Curtidas 0

Melhor post

Eduardo Chaves

Eduardo Chaves

10/08/2017

Hi Lucio.

Maybe this will solve your problem:

myTableView.setRowFactory (tv ->
{
TableRow<ObservableList> myRow = new TableRow<>();
theRow.setOnMouseClicked (event ->
{
if (event.getClickCount() == 1 && (!myRow.isEmpty()))
{
int myIndex =
myTableView.getSelectionModel().getSelectedIndex();

String myRowData =
myTableView.getItems().get(myIndex).toString();
}
});
return myRowData;
});


On your code you set the string "myRowData" but you return "myRow". So I changed your return to "myRowData".

Hope it helps.
GOSTEI 1
POSTAR