简体   繁体   中英

JavaFx - values from TableColumn do not change

I'm trying to get the value of the editable table using javaFX Scene builder, but I can not get the updated value.

I' have table with 2 columns: name column and value column. whenever the user change on of the value of value column, I want to react.

But when I print the new change value, it always displays the default value.

public class MesssageField
{

    private final StringProperty fieldName;
    private final StringProperty fieldValue;

    public MesssageField(String fieldName, String fieldValue) {
        this.fieldName = new SimpleStringProperty(fieldName);
        this.fieldValue = new SimpleStringProperty(fieldValue);        
    }


    public StringProperty getFieldNameProperty() {
        return fieldName;
    }

    public StringProperty getFieldValueProperty() {
        return fieldValue;
    }

    public void setFieldValue(String fieldValue) {
        this.fieldValue.set(fieldValue);
    }

    public String getFieldName() {
        return fieldName.get();
    }


    public String getFieldValue() {
        return fieldValue.get();
    }

}

The controller class has:

private static ObservableList<MesssageField> obserListMsgsField;

@FXML
private TableView<MesssageField> msgTableView;

@FXML
private TableColumn<MesssageField, String> fieldNameColumn;

@FXML
private TableColumn<MesssageField, String> fieldValueColumn;

@Override
public void initialize(URL arg0, ResourceBundle arg1)
{
    // create List Of fields
    obserListMsgsField = FXCollections.observableArrayList();

    fieldValueColumn.setCellFactory(TextFieldTableCell.forTableColumn());

    // Initialize the person table with the two columns.
    fieldNameColumn.setCellValueFactory(cellData -> cellData.getValue().getFieldNameProperty());
    fieldValueColumn.setCellValueFactory(cellData -> cellData.getValue().getFieldValueProperty());  
}

the onEditFieldValueCommit method attached to "on edit commit"

@FXML
public void onEditFieldValueCommit() {

    MesssageField messageField = msgTableView.getSelectionModel().getSelectedItem();

    // get field name
    String fieldName = messageField.getFieldName();

    // get field value       
    String valueString = messageField.getFieldValue(); 

    // debug print
    System.out.print("\n[DEBUG] Field Name = " + fieldName + " = " + valueString);  
}

But the output is always the default value and not the changed value.

Thanks

In your onEditFieldValueCommit method you are not using the parameter required, and then you are not updating the list obserListMsgsField .

First, you need to add a parameter of the type TableColum.CellEditEvent , which is the event that is fired when a user performs an edit on the table cell.

Then you just get the new value or the row affected, updating the list:

@FXML
public void onEditFieldValueCommit(TableColumn.CellEditEvent<MesssageField, String> t) {
    t.getRowValue().setFieldValue(t.getNewValue());

    System.out.print("\n[DEBUG] Field Name = " + t.getRowValue().getFieldName() + 
            " = " + t.getRowValue().getFieldValue());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM