TableView上的JavaFx CheckBox在选中/取消选中时不会更新模型 [英] JavaFx CheckBox on TableView does not update model when checked/unchecked

查看:1200
本文介绍了TableView上的JavaFx CheckBox在选中/取消选中时不会更新模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有复选框的第一列的TableView。

i have a TableView with the first column with a check box.

我设法正确显示复选框并使其可编辑,但当我点击复选框时要检查或取消选中,复选框会更改,但它不会更新我的模型。

I managed to show the checkbox correctly and make it editable, but when i click on the checkbox to check or uncheck, the checkBox changes but it does not update my model.

这就是我这样做的方式:

This is how i did it:

TableColumn<ContasReceber, Boolean> myCheckBoxColumn = (TableColumn<ContasReceber, Boolean>) tabelaContas.getColumns().get(0);
myCheckBoxColumn.setCellFactory(p -> new CheckBoxTableCell<>());
myCheckBoxColumn.setOnEditCommit(evt -> evt.getRowValue().setChecked(evt.getNewValue()));//It never executes the method setChecked when i click on the checkBox to change it's values.


推荐答案

我发现自己做的方式,这是相当的很简单,我在这个问题中回答
有一种非常简单的方法,你不需要用SimpleBooleanProperty或其他任何东西来修改你的模型类,只需按照以下步骤操作:

I found i way of doing it myself, it's quite simple as i answered in this other question: There is a very simple way of doing this, you don't need to modify your model class with SimpleBooleanProperty or whatever, just follow these steps:

1 - 假设您有一个带有isUnemployed方法的Person对象:


public class Person {
    private String name;
    private Boolean unemployed;

    public String getName(){return this.name;}
    public void setName(String name){this.name = name;}
    public Boolean isUnemployed(){return this.unemployed;}
    public void setUnemployed(Boolean unemployed){this.unemployed = unemployed;}
}

2 - 创建回调类


import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableColumn;
import javafx.util.Callback;

public class PersonUnemployedValueFactory implements Callback, ObservableValue> {
    @Override
    public ObservableValue call(TableColumn.CellDataFeatures param) {
        Person person = param.getValue();
        CheckBox checkBox = new CheckBox();
        checkBox.selectedProperty().setValue(person.isUnemployed());
        checkBox.selectedProperty().addListener((ov, old_val, new_val) -> {
            person.setUnemployed(new_val);
        });
        return new SimpleObjectProperty(checkBox);
    }
}

3 - 将回调绑定到表格专栏

如果您使用FXML,请将回调类放在您的列中:

If you use FXML, put the callback class inside your column:

<TableView fx:id="personList" prefHeight="200.0" prefWidth="200.0">
    <columns>
        <TableColumn prefWidth="196.0" text="Unemployed">
            <cellValueFactory>
                <PersonUnemployedValueFactory/> <!--This is how the magic happens-->
            </cellValueFactory>
        </TableColumn>

        ...
    </columns>
</TableView>

不要忘记在FXML中导入课程:

Don't forget to import the class in your FXML:

<?import org.yourcompany.yourapp.util.PersonUnemployedValueFactory?>

如果没有FXML,请执行以下操作:

Without FXML, do it like this:

TableColumn<Person, CheckBox> column = (TableColumn<Person, CheckBox>) personTable.getColumns().get(0);
column.setCellValueFactory(new PersonUnemployedValueFactory());

4 - 就是这样

所有内容都应按预期工作,当您单击复选框时将值设置为辅助bean,并在表中加载项目列表时正确设置复选框值。

Everything should work as expected, with the value being set to the backing bean when you click on the checkbox, and the checkbox value correctly being set when you load the items list in your table.

这篇关于TableView上的JavaFx CheckBox在选中/取消选中时不会更新模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆