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); }
Advertisement
Answer
Just do
firstNameCol.setCellFactory(TextFieldTableCell.forTableColumn()); firstNameCol.setOnEditCommit(event -> { String[] row = event.getRowValue(); row[index] = event.getNewValue(); });