表单元格组合框 - 未执行操作 [英] Table cell combobox - action not performed

查看:137
本文介绍了表单元格组合框 - 未执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将以下单元格工厂应用到列。

I'm applying the below cell factory to a column.

    targetEnviroment.setCellFactory(new Callback<TableColumn<DevWorkTabBench, String>, TableCell<DevWorkTabBench, String>>() {

                @Override
                public TableCell<DevWorkTabBench, String> call(TableColumn<DevWorkTabBench, String> param) {
                    TableCell<DevWorkTabBench, String> cell = new TableCell<DevWorkTabBench, String>() {
                        @Override
                        public void updateItem(String item, boolean empty) {
                            super.updateItem(item, empty);

                            String status = null;
                            try {
                                status = getTableView().getItems().get(getIndex()).getObjectStatus();
                            } catch (IndexOutOfBoundsException ex) {
                            status = "";
                            }
                            if (status.equalsIgnoreCase("ReadyForDeployment")) {

                                ComboBox<String> comboBox = new ComboBox(environmentList);
                                comboBox.valueProperty().addListener(new ChangeListener<String>() {

                                    @Override
                                    public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                                        commitEdit(newValue);
                                    }
                                });

                                comboBox.setOnShown(new EventHandler<Event>() {
                                    @Override
                                    public void handle(Event event) {
                                        getTableView().edit(getIndex(), getTableColumn());
                                        getTableView().getSelectionModel().select(getIndex());

                                    }
                                });
                                comboBox.setValue(item);
                                setGraphic(comboBox);
                            } else {
                                setGraphic(null);
                            }

                            if (empty) {
                                setGraphic(null);

                            }
                        }
                    };

                    return cell;
                }

            });

当我将状态状态,我看到 ComboBox 在该特定单元格,但下拉不发生。即使多次点击后,似乎不会在组合框上执行任何操作。我没有得到任何异常,除了处理一个。其他列可按预期编辑和执行任务。

When I change the status to the mentioned status, I get the look of ComboBox in that particular cell but the drop down does not occur. Even after multiple clicks no action seems to be performed on the combobox. I do not get any exception other than the handled one. Other columns are editable and performing task as expected.

我不知道这里有什么问题。

I have no idea what is wrong here. Can anyone please help me.

推荐答案

因为你总是在(非空)单元格中显示组合框, 't真的需要进入编辑模式作为标准 TextFieldTableCell 等。您的实现更类似于 CheckBoxTableCell ,这本质上绕过了编辑机制。从该课程的文档

Since you are always displaying the combo box in the (non-empty) cells, you don't really need to go into "editing" mode as the standard TextFieldTableCell etc does. Your implementation is more similar to the CheckBoxTableCell, which essentially bypasses the editing mechanism. From the documentation for that class:


请注意,CheckBoxTableCell呈现CheckBox'live',意味着
,CheckBox始终是交互式的,由
切换为用户。这意味着单元格不必要进入其
编辑状态(通常由用户双击单元格)。 A
的副作用是,通常的编辑回调(如对
编辑提交)不会被调用。如果你想要通知
变化,建议直接观察由CheckBox操作的布尔属性

Note that the CheckBoxTableCell renders the CheckBox 'live', meaning that the CheckBox is always interactive and can be directly toggled by the user. This means that it is not necessary that the cell enter its editing state (usually by the user double-clicking on the cell). A side-effect of this is that the usual editing callbacks (such as on edit commit) will not be called. If you want to be notified of changes, it is recommended to directly observe the boolean properties that are manipulated by the CheckBox.

所以你的单元实现的行为类似:不调用 edit(...)(我认为是搞乱了)在 commitEdit(...) cancelEdit()等方法不在编辑状态),但只是直接更新模型类。

So your cell implementation behaves similarly: don't invoke edit(...) (which I think is messing things up) and don't rely on the commitEdit(...), cancelEdit() etc methods (which won't work as you're not in editing state), but just update the model class directly.

我无法测试,因为没有一个MCVE工作,所以这可能不

I can't test, since there isn't a MCVE to work from, so this might not work directly, but it should be enough to get you started toward something that will work.

targetEnviroment.setCellFactory(new Callback<TableColumn<DevWorkTabBench, String>, TableCell<DevWorkTabBench, String>>() {

    @Override
    public TableCell<DevWorkTabBench, String> call(TableColumn<DevWorkTabBench, String> param) {
        TableCell<DevWorkTabBench, String> cell = new TableCell<DevWorkTabBench, String>() {
            @Override
            public void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);

                if (empty) {
                    setGraphic(null) ;
                } else {
                    String status = getTableView().getItems().get(getIndex()).getObjectStatus();

                    if (status.equalsIgnoreCase("ReadyForDeployment")) {

                        ComboBox<String> comboBox = new ComboBox(environmentList);
                        comboBox.valueProperty().addListener(new ChangeListener<String>() {

                            @Override
                            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                                //commitEdit(newValue);
                                getTableView().getItems().get(getIndex()).setObjectStatus(newValue);
                            }
                        });

                        comboBox.setValue(item);
                        setGraphic(comboBox);
                    } else {
                        setGraphic(null);
                    }
                }
            }
        };

        return cell;
    }

});

这篇关于表单元格组合框 - 未执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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