0

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);
    }
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
ALSTRA
  • 661
  • 3
  • 12
  • 30

2 Answers2

2

Just do

firstNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
firstNameCol.setOnEditCommit(event -> {
    String[] row = event.getRowValue();
    row[index] = event.getNewValue();
});
James_D
  • 201,275
  • 16
  • 291
  • 322
0

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;
NaveenBharadwaj
  • 1,212
  • 5
  • 19
  • 42
  • What should I add in every <> ? Because IntelliJ says: "Identifier expected" – ALSTRA Jul 14 '15 at 10:44
  • Try to create a model class (POJO class) corresponding to each column in your table. Use that model class between <> – NaveenBharadwaj Jul 15 '15 at 04:43
  • This should be a dynamic tableview, so I dont know how much columns I have. Thats why I cant create a model class ... – ALSTRA Jul 15 '15 at 06:49
  • what is cnv.getColumns()? you cannot be unsure of the columns that you get. At least you must be having a set of all possible columns that you may get. You need to include all of them in POJO class and populate only the ones which get generated dynamically. – NaveenBharadwaj Jul 15 '15 at 07:20
  • cnv.getColumns() gives you the number of the columns (if they are 6 colums, it returns the (int) 6)....but i dont understand what u meand with POJO class etc.. .-. – ALSTRA Jul 15 '15 at 07:51
  • http://stackoverflow.com/questions/11722251/what-is-pojo-dojo-in-java Here's a Pojo example. In this case, one object of this class will represent one row in your table. Members of this class will correspond to each column in your table. – NaveenBharadwaj Jul 15 '15 at 08:17
  • Create JSFiddle of your example if you need help. Please upvote and accept the answer. – NaveenBharadwaj Jul 15 '15 at 08:20
  • I cant create something like this, because i dont know how many calumns i will have. The tableview is dynamic, it reads the data from a text file. – ALSTRA Jul 15 '15 at 08:38
  • @ALSTRA I think you have confused readers by calling the column `firstNameCol`, which sounds like a column which maps to a property with the name `firstName`, belonging to some object. Why did you call the column this in this example? Since the model class for this table is `String[]`, it makes no sense at all to call it that. – James_D Jul 15 '15 at 13:48
  • I just forgot to rename it, because I have taken this code from a other programm – ALSTRA Jul 15 '15 at 13:57