napstyr maceda
napstyr maceda

Reputation: 179

JavaFX: Get the selected row cell's text

I need your help. I am developing JavaFX project that deals with table view and forms. My Problem is I can't get the text of the selected row in the table view. I want to get the row cell's text using the row index or the selected row one.

Here is my code:

myTableView.getSelectionModel().selectedItemProperty().addListener( new ChangeListener() {
    @Override
    public void changed(ObservableValue ov, Object t, Object t1) {

        TableView.TableViewSelectionModel selectionModel = myTableView.getSelectionModel();
        ObservableList selectedCells = selectionModel.getSelectedCells();
        TablePosition tablePosition = (TablePosition) selectedCells.get(0);
        int rowIndex = tablePosition.getRow(); // yields the row that the currently selected cell is in 

        // I Want to get the cell's text in the row using the row_index or the selected row one
    }
});

Any solution is appreciated. Thank you!

Upvotes: 2

Views: 3046

Answers (1)

assylias
assylias

Reputation: 328815

If you only care about which row is selected, assuming you have a TableView<SomeObject>, you can simply use:

List<SomeObject> selected = selectionModel.getSelectedItems();

or if your table only allows single row selection:

SomeObject selected = selectionModel.getSelectedItem();

Upvotes: 4

Related Questions