在JavaFX中的表中获取Checkbox值 [英] Get Checkbox value in a table in JavaFX

查看:1492
本文介绍了在JavaFX中的表中获取Checkbox值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子,它有一个带复选框的列。

I have a table and it has a column with checkboxes.

点击一下按钮我想知道哪些复选框被选中,哪些不是。
到目前为止,我设法在表格中创建复选框。
代码如下。

On a button click I want to find out which checkboxes are checked and which are not. So far I managed to create checkboxes in a table. The code is as follows.

public class TTEs implements Initializable {

    @FXML
    private TableView<TestObject> tableReport;

    @FXML
    private TableColumn<TestObject, String> name;

    @FXML
    private TableColumn<TestObject, Boolean> checkbox;

    @FXML
    public void getValues() {        
        //the method will get what check boxes are checked (this part is the problem)
    }

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) { 
        ObservableList<TestObject> data = FXCollections.observableArrayList();
        data.add(new TestObject("Test 1", true));
        data.add(new TestObject("Test 2", false));

        tableReport.setItems(data);

        name.setCellValueFactory(new PropertyValueFactory<TestObject, String>("name"));
        checkbox.setCellValueFactory(new PropertyValueFactory<TestObject, Boolean>("checked"));

        checkbox.setCellFactory(new Callback<TableColumn<TestObject, Boolean>,
            TableCell<TestObject, Boolean>>() {

            public TableCell<TestObject, Boolean> call(TableColumn<TestObject, Boolean> p) {
                return new CheckBoxTableCell<TestObject, Boolean>();
            }
        });
    }

    //CheckBoxTableCell for creating a CheckBox in a table cell
    public static class CheckBoxTableCell<S, T> extends TableCell<S, T> {
        private final CheckBox checkBox;
        private ObservableValue<T> ov;

        public CheckBoxTableCell() {
            this.checkBox = new CheckBox();
            this.checkBox.setAlignment(Pos.CENTER);

            setAlignment(Pos.CENTER);
            setGraphic(checkBox);
        } 

        @Override public void updateItem(T item, boolean empty) {
            super.updateItem(item, empty);
            if (empty) {
                setText(null);
                setGraphic(null);
            } else {
                setGraphic(checkBox);
                if (ov instanceof BooleanProperty) {
                    checkBox.selectedProperty().unbindBidirectional((BooleanProperty) ov);
                }
                ov = getTableColumn().getCellObservableValue(getIndex());
                if (ov instanceof BooleanProperty) {
                    checkBox.selectedProperty().bindBidirectional((BooleanProperty) ov);
                }
            }
        }
    }
}

当我调试时,我发现:

        ov = getTableColumn().getCellObservableValue(getIndex());
        if (ov instanceof BooleanProperty) {
            checkBox.selectedProperty().bindBidirectional((BooleanProperty) ov);
        }

在上述情况下,它永远不会进入 if 语句,意味着 ov 不是 BooleanProperty 的实例。但是当我打印 ov 的类时,

In the above condition, it never goes inside the if statement, meaning that the ov is not an instance of BooleanProperty. But when I print the class of ov,

System.out.println(ov.getClass().getName());

打印为

javafx.beans.property.ReadOnlyObjectWrapper

ReadOnlyObjectWrapper BooleanProperty 的子类,那么为什么 instanceof 检查不起作用?

ReadOnlyObjectWrapper is a subclass of BooleanProperty, so why is the instanceof check not working?

推荐答案

在Java 8上测试。

只有4个简单的东西。

Tested on Java 8.
Only 4 simple things.

1)制作CheckBoxCellFactory类。把它放在你的项目中。

public class CheckBoxCellFactory implements Callback {
    @Override
    public TableCell call(Object param) {
        CheckBoxTableCell<Person,Boolean> checkBoxCell = new CheckBoxTableCell();
        return checkBoxCell;
    }
}

2)您的模型类。例如。

public static class Person {
   private SimpleBooleanProperty checked = new SimpleBooleanProperty(false);
   // other columns here

    public SimpleBooleanProperty checkedProperty() {
        return this.checked;
    }

    public java.lang.Boolean getChecked() {
        return this.checkedProperty().get();
    }

    public void setChecked(final java.lang.Boolean checked) {
        this.checkedProperty().set(checked);
    }

    // getter/setter for other columns

}

3)在fxml文件中进行了修改。 TableView - > TableColumn的部分将如下所示:

<TableColumn fx:id="checkBoxTableColumn" maxWidth="34.0" minWidth="26.0" prefWidth="34.0" resizable="false" sortable="false">   
<cellValueFactory><PropertyValueFactory property="checked" /></cellValueFactory>
<cellFactory><partarch.fx.CheckBoxCellFactory /></cellFactory>
</TableColumn>

4)如果您想使复选框可编辑


  • 4.1在场景生成器中打开fxml。

  • 4.2选择checkBox列,然后选中Editable属性in 属性窗格。

  • 4.3选择包含复选框的TableView并执行相同操作(在属性窗格中选中可编辑属性)。

SF 的二进制文件中实施和来自 GitHub

这篇关于在JavaFX中的表中获取Checkbox值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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