加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

如何将CheckBox添加到JavaFX中的TableView

发布时间:2020-12-14 21:40:25 所属栏目:资源 来源:网络整理
导读:在我的Java桌面应用程序中,我有一个TableView,其中我想要一个带有CheckBox的列。 我确实发现这已经完成了http://www.jonathangiles.net/javafx/2.0/CellFactories/,但由于下载不可用,因为我不知道Jonathan Giles将如何回答我的电子邮件,我以为我会问…
在我的Java桌面应用程序中,我有一个TableView,其中我想要一个带有CheckBox的列。

我确实发现这已经完成了http://www.jonathangiles.net/javafx/2.0/CellFactories/,但由于下载不可用,因为我不知道Jonathan Giles将如何回答我的电子邮件,我以为我会问…

如何将CheckBox放在TableView的单元格中?

解决方法

您需要在TableColumn上设置一个CellFactory。

例如:

Callback<TableColumn<TableData,Boolean>,TableCell<TableData,Boolean>> booleanCellFactory = 
            new Callback<TableColumn<TableData,Boolean>>() {
            @Override
                public TableCell<TableData,Boolean> call(TableColumn<TableData,Boolean> p) {
                    return new BooleanCell();
            }
        };
        active.setCellValueFactory(new PropertyValueFactory<TableData,Boolean>("active"));
        active.setCellFactory(booleanCellFactory);

class BooleanCell extends TableCell<TableData,Boolean> {
        private CheckBox checkBox;
        public BooleanCell() {
            checkBox = new CheckBox();
            checkBox.setDisable(true);
            checkBox.selectedProperty().addListener(new ChangeListener<Boolean> () {
                public void changed(ObservableValue<? extends Boolean> observable,Boolean oldValue,Boolean newValue) {
                    if(isEditing())
                        commitEdit(newValue == null ? false : newValue);
                }
            });
            this.setGraphic(checkBox);
            this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
            this.setEditable(true);
        }
        @Override
        public void startEdit() {
            super.startEdit();
            if (isEmpty()) {
                return;
            }
            checkBox.setDisable(false);
            checkBox.requestFocus();
        }
        @Override
        public void cancelEdit() {
            super.cancelEdit();
            checkBox.setDisable(true);
        }
        public void commitEdit(Boolean value) {
            super.commitEdit(value);
            checkBox.setDisable(true);
        }
        @Override
        public void updateItem(Boolean item,boolean empty) {
            super.updateItem(item,empty);
            if (!isEmpty()) {
                checkBox.setSelected(item);
            }
        }
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读