在JavaFX中的表中获取Checkbox值

 kg810219_447 发布于 2023-02-07 09:52

我有一个表,它有一个带复选框的列.

到目前为止的输出

在按钮上单击,我想找出哪些复选框被选中,哪些不是.到目前为止,我设法在表格中创建复选框.代码如下.

public class TTEs implements Initializable {

    @FXML
    private TableView tableReport;

    @FXML
    private TableColumn name;

    @FXML
    private TableColumn 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 data = FXCollections.observableArrayList();
        data.add(new TestObject("Test 1", true));
        data.add(new TestObject("Test 2", false));

        tableReport.setItems(data);

        name.setCellValueFactory(new PropertyValueFactory("name"));
        checkbox.setCellValueFactory(new PropertyValueFactory("checked"));

        checkbox.setCellFactory(new Callback,
            TableCell>() {

            public TableCell call(TableColumn p) {
                return new CheckBoxTableCell();
            }
        });
    }

    //CheckBoxTableCell for creating a CheckBox in a table cell
    public static class CheckBoxTableCell extends TableCell {
        private final CheckBox checkBox;
        private ObservableValue 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,

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

它打印为

javafx.beans.property.ReadOnlyObjectWrapper

ReadOnlyObjectWrapper是一个子类BooleanProperty,为什么instanceof检查不起作用?

2 个回答
  • 在Java 8上测试过.
    只有4个简单的东西.

    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" max min pref resizable="false" sortable="false">   
    <cellValueFactory><PropertyValueFactory property="checked" /></cellValueFactory>
    <cellFactory><partarch.fx.CheckBoxCellFactory /></cellFactory>
    </TableColumn>
    

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

    4.1在Scene Builder中打开fxml.

    4.2选择checkBox列,然后在"属性"窗格中选中"可编辑"属性.

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

    SF上的二进制文件和GitHub上的源代码中实现

    2023-02-07 09:57 回答
  • 循环TableView检查绑定到每个的布尔值的数据模型CheckBox.

    @FXML
    public void getValues(){        
        ObservableList<TestObject> data = tableReport.getItems();
    
        for (TestObject item : data){
            //check the boolean value of each item to determine checkbox state
        }
    }
    

    2023-02-07 09:58 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有