DeluxeD
DeluxeD

Reputation: 143

JavaFX: TableView - get selected Item column values (String) into TextFields

I am making an app which has a TableView Content and I would like to select every selected row cell data into TextFields. I started with this question: Getting selected item from a JavaFX TableViewAsk

But I only can get my first column of these data. My question is how can I get the second and third column as well? Here is my code:

table.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
        @Override
        public void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
            if (table.getSelectionModel().getSelectedItem() != null) {
                TableViewSelectionModel selectionModel = table.getSelectionModel();
                ObservableList selectedItem = selectionModel.getSelectedCells();
                TablePosition getName = (TablePosition) selectedItem.get(0);
                Object val1 = getName.getTableColumn().getCellData(newValue);
                name.setText("" + val1);
            }
        }
    });

Upvotes: 0

Views: 2402

Answers (3)

FARS
FARS

Reputation: 457

Just use the following piece of code:

IMPORTANT: Note that this method should be implemented in the "On Mouse Clicked" event of your TableView, check the image below for reference:

enter image description here

Integer index = -1;
@FXML
private void showRowDataInTextFields()
    {
        index = tableViewName.getSelectionModel().getSelectedIndex();
        if (index <= -1)
        {
            return;
        }
        textBoxName.setText(column_Name.getCellData(index)); // used for columns which has only strings
        textBoxNameWithNumber.setText(column_Name.getCellData(index).toString()); // use this one for columns which contains numbers
    }

Edit: code improvement

Upvotes: 0

James_D
James_D

Reputation: 209674

The newItem in your listener method is the object representing the entire row in the table.

Just type the change listener properly, and then you have access to all the data in the row. I.e. assuming you defined

TableView<T> table ;

for some class T, then just use the following (replacing T with whatever class you used in your table view):

table.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<T>() {
    @Override
    public void changed(ObservableValue<? extends T> observableValue, T oldValue, T newValue) {
        // now just call methods on newValue to get the data you need...
    }
});

Upvotes: 0

Keyur Bhanderi
Keyur Bhanderi

Reputation: 1544

Selected item property is used when the selection model is set to be single selection, So you can define a rowFactory.

Object cellItem = cell.getTableRow().getItem();

this will give you row item of table.So you can do like that

I hope below code is working for you.

table.setRowFactory(tv -> {
    TableRow<DataType> row = new TableRow<>();
        row.setOnMouseClicked(event -> {
            if (!row.isEmpty()) {
                DataType clickedRow = row.getItem();
                TableViewSelectionModel selectionModel = table.getSelectionModel();
                ObservableList selectedItem = selectionModel.getSelectedCells();
                TablePosition getName = (TablePosition) selectedItem.get(0);
                Object val1 = getName.getTableColumn().getCellData(clickedRow);
                name.setText("" + val1);
                System.out.println("selected :" + val1);    
                }
            });
        return row;
});

Upvotes: 1

Related Questions