CheckBoxTableCell changelistener无法正常工作 [英] CheckBoxTableCell changelistener not working

查看:135
本文介绍了CheckBoxTableCell changelistener无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向CheckBoxTableCells添加一个更改侦听器,但它似乎没有工作。我以CheckBoxes为例,确定它们的工作方式是否相同。但是,当我更改其值时,没有输出。我如何正确地将一个正确添加到checkboxtablecell?

I'm trying to add a change listener to my CheckBoxTableCells but it doesn't seem to be working. I took the example for CheckBoxes figuring they would work the same way. However there is no output when i change its value. How would i add one correctly to a checkboxtablecell?

当前代码:

tc.setCellFactory(new Callback<TableColumn<Trainee, Boolean>, TableCell<Trainee, Boolean>>() {
                    @Override
                    public TableCell<Trainee, Boolean> call(TableColumn<Trainee, Boolean> p) {
                        final CheckBoxTableCell ctCell = new CheckBoxTableCell<>();
                        ctCell.selectedProperty().addListener(new ChangeListener<Boolean>() {
                            @Override
                            public void changed(ObservableValue ov, Boolean old_val, Boolean new_val) {
                                System.out.println(new_val);
                            }
                        });
                        return ctCell;
                    }
                });


推荐答案

selectedProperty 继承自 Cell ,它只是指示是否在UI组件中选择了 Cell 。由于您可能没有在 TableView 上启用单元格选择,因此单元格永远不会被选中。这不是你想要的东西;你想知道是否选择了 CheckBox ,而不是 Cell

The selectedProperty is inherited from Cell and it just indicates whether the Cell is selected in the UI component. Since you probably don't have cell selection enabled on your TableView, the cell never becomes selected. This isn't what you're looking for anyway; you want to know whether the CheckBox is selected, not the Cell.

这里的诀窍是使用 CheckBoxTableCell selectedStateCallback 属性。这是一个将单元格的索引映射到 BooleanProperty 的函数。 BooleanProperty 双向绑定到复选框的选定状态。

The trick here is to use the selectedStateCallback property of the CheckBoxTableCell. This is a function that maps the index of the cell to a BooleanProperty. That BooleanProperty is bound bidirectionally to the selected state of the check box.

如果您的列代表实际属性在您的实习生课程中(我只是将其称为 selectedProperty 进行演示)然后您可以执行以下操作:

If your column is representing an actual property in your Trainee class (I'll just call it selectedProperty for demonstration) then you can do something like this:

final CheckBoxTableCell<Trainee, Boolean> ctCell = new CheckBoxTableCell<>();
ctCell.setSelectedStateCallback(new Callback<Integer, ObservableValue<Boolean>>() {
    @Override
    public ObservableValue<Boolean> call(Integer index) {
        return table.getItems().get(index).selectedProperty();
    }
});

然后实习生课程中的属性双向绑定到复选框状态。如果您需要做的不仅仅是在选中/取消选中复选框时更新模型对象,您可以只观察该属性。

Then the property in the Trainee class with be bidirectionally bound to the check box state. If you need to do more than just update your model object when the check box is selected/deselected, you can just observe that property.

如果您没有实习生课程中的属性,你可以创建一个 BooleanProperty 并观察它:

If you don't have a property in the Trainee class, you can just create a BooleanProperty and observe it:

final CheckBoxTableCell<Trainee, Boolean> ctCell = new CheckBoxTableCell<>();
final BooleanProperty selected = new SimpleBooleanProperty();
ctCell.setSelectedStateCallback(new Callback<Integer, ObservableValue<Boolean>>() {
    @Override
    public ObservableValue<Boolean> call(Integer index) {
        return selected ;
    }
});
selected.addListener(new ChangeListener<Boolean>() {
    @Override
    public void changed(ObservableValue<? extends Boolean> obs, Boolean wasSelected, Boolean isSelected) {
        System.out.println(isSelected);
    }
});

像往常一样,所有这些代码在Java 8中都看起来好多了。

As usual, all this code looks a lot nicer in Java 8.

这篇关于CheckBoxTableCell changelistener无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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