DeBiA
DeBiA

Reputation: 25

Javafx: updating cell using choicebox in the GUI doesn't work

I have a problem in a Tableview created using javafx. I have set the edititable="true" on the fxml file of the tabel, then in the controller I execute

@FXML
private TableColumn<ARule,Object> rankCol;

rankCol.setCellValueFactory(new PropertyValueFactory<ARule, Object>("label")); rankCol.setCellFactory(ChoiceBoxTableCell.forTableColumn(Main.getlabelSample())); rankCol.setOnEditCommit(e -> {System.out.println("something happens!");});

To create in the column rank, a choicebox to change te value of the property. The ARule has a property field and the getter and setters:

private SimpleObjectProperty label;
public SimpleObjectProperty labelProperty() {
    return label;
}
public void setLabel(Object label) {
    this.label.set(label);
}
public Object getLabel(){
    return this.label.getValue();
}

The function Main.getlabelSample() retrun this object filled with Strings or Integer

private static final ObservableList<Object> labelSample = FXCollections.observableArrayList();

The problem is that in the interface I can edit the column and it displays the correct value in the labelSample list, the problem is that it doesn't change the value of the ARule object, this is highlighted by the missing call of the setOnEditCommit handler. The value on the GUI is the new one selected but the value saved on the items in the table is the old one.

I have also a separated button to change the value of that column on the selected row and if I trigger that, the values changes for "real" (both on the GUI and on the model).

What could be the error in the code?

Upvotes: 2

Views: 286

Answers (2)

James_D
James_D

Reputation: 209674

The default edit commit behavior of the column is set as the onEditCommit property. If you call

rankCol.setOnEditCommit(...);

then you set this property to something else, i.e. you remove the default behavior.

If you want to add additional behavior to the default, use addEventHandler(...) instead of setOnEditCommit(...):

rankCol.addEventHandler(TableColumn.editCommitEvent(), e -> {
    System.out.println("Something happens");
});

Upvotes: 1

DeBiA
DeBiA

Reputation: 25

Find the answer the line of code:

rankCol.setOnEditCommit(e -> {System.out.println("something happens!");});

for some reason overwrite the default behaviour of updating the cell changing the code into

rankCol.setOnEditCommit(e -> {
        e.getTableView().getItems().get(e.getTablePosition().getRow()).setLabel(e.getNewValue());
        System.out.println("Something happens!");});

Resolved the problem. At the moment I don't know why this is happening.

Upvotes: 0

Related Questions