Table 单元格组合框 - 未执行操作

Table cell combobox - action not performed

我正在将下面的细胞工厂应用于列。

    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;
                }

            });

当我将 status 更改为上述状态时,我在该特定单元格中看到 ComboBox 的外观,但不会出现下拉列表。即使在多次点击后,combobox 似乎也没有执行任何操作。除了处理的异常,我没有得到任何异常。其他列可编辑并按预期执行任务。

我不知道这里出了什么问题。谁能帮帮我。

由于您总是在(非空)单元格中显示组合框,因此您实际上不需要像标准 TextFieldTableCell 等那样进入 "editing" 模式。您的实现更类似于 CheckBoxTableCell,它基本上绕过了编辑机制。来自 documentation for that class:

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() 等方法(这不会工作,因为你不在编辑状态),但直接更新模型 class。

我无法测试,因为没有 MCVE 可以工作,所以这可能无法直接工作,但它应该足以让你开始做一些有用的事情。

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;
    }

});