ALSTRA
ALSTRA

Reputation: 661

Edit the cell value at a dynamic TableView?

Is it possible to edit a cell value in a dynamic TableView (dynamic rows and dynamic columns)?

All I found on the internet was some editable TextFields over the cells.
However, I want to edit the value in the table and then update my List with the new data.

I'm using IntelliJ IDEA 13.1.4 , JavaFX Scene Builder 2.0 and the newest JavaFX version.

Here is the code, where I create the dynamic rows and columns:

public List<String[]> jdata = new LinkedList<>(); //Here is the data
private TableView<String[]> sourceTable;
private ObservableList<String[]> srcData;
.
.
.

int clms;
    
    public void showTable(Convert cnv) {
        clms = cnv.getColums(); //number of the columns

        for (int i = 0; i < clms; i++) {
            TableColumn<String[], String> firstNameCol = new TableColumn<>("\tC"+(i+1)+" \t");
            firstNameCol.setMinWidth(20);
            int index = i ;
            firstNameCol.setCellValueFactory(cellData -> {
                String[] rowData = cellData.getValue();
                if (index >= rowData.length) {
                    return new ReadOnlyStringWrapper("");
                } else {
                    String cellValue = rowData[index];
                    return new ReadOnlyStringWrapper(cellValue);
                }
            });
            sourceTable.getColumns().add(firstNameCol);
        }
        srcData = FXCollections.observableList(jdata);
        sourceTable.getItems().addAll(srcData);
    }

Upvotes: 0

Views: 7167

Answers (2)

James_D
James_D

Reputation: 209225

Just do

firstNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
firstNameCol.setOnEditCommit(event -> {
    String[] row = event.getRowValue();
    row[index] = event.getNewValue();
});

Upvotes: 2

NaveenBharadwaj
NaveenBharadwaj

Reputation: 1333

This code will make the firstNameCol column editable. When you click on any cell under this column, you will get a TextField where you can enter value. When you hit enter, the value gets saved in the table.

UPDATE: Let us say you have created a model class for your Table, and lets assume its name is TestCasesModel, this is how the above code would look.

firstNameCol.setCellFactory(TextFieldTableCell.<TestCasesModel>forTableColumn());
        firstNameCol.setOnEditCommit(
                new EventHandler<CellEditEvent<TestCasesModel, String>>() {
                    @Override
                    public void handle(CellEditEvent<TestCasesModel, String> t) {
                        ((TestCasesModel) t.getTableView().getItems().get(
                                t.getTablePosition().getRow())
                                ).setObjectName(t.getNewValue());
                    }
                }
                );

It is always a good practice to work with POJO classes instead of String arrays. CellEditEvent must be imported like this:

import javafx.scene.control.TableColumn.CellEditEvent;

Upvotes: 0

Related Questions