将整个 ComboBox 放在 TableView 列中

Put whole ComboBox in TableView column

The Java Swing run imageThe JavaFx run image我需要将整个 ComboBox 添加到 TableView 列,而不是 JavaFx 中的 OservableArrayList,因为我将自动完成 属性 设置为像 java swing 这样的 ComboBox,我尝试使用 java swing 并成功运行,但在 javafx

中不起作用
@Override
public void start(Stage primaryStage) {
    StackPane root = new StackPane();
    Scene scene = new Scene(root, 300, 250);
    TableView table = new TableView();
    ObservableList <String> list = FXCollections.observableArrayList();
    ComboBox comboBox = new ComboBox();

    list.add("Product One");
    list.add("Product Two");
    list.add("Product Three");
    list.add("Product Four");

    table.setEditable(true);
    comboBox.setEditable(true);

    comboBox.setItems(list);

    TextFields.bindAutoCompletion(comboBox.getEditor(), comboBox.getItems());

    TableColumn productName = new TableColumn("Product Name");
    TableColumn productCode = new TableColumn("Product Code");

    productName.setEditable(true);
    productName.setCellValueFactory(ComboBoxTableCell.forTableColumn(list));

    productName.setMinWidth(100);
    productCode.setMinWidth(100);

    table.getColumns().addAll(productName, productCode);
    table.getItems().add(new Object [] {null, null});

    root.getChildren().add(table);

    primaryStage.setTitle("Test");
    primaryStage.setScene(scene);
    primaryStage.show();
}

首先,您必须为 cellFactory 而不是 cellValueFactory 设置 ComboBoxTableCell.forTableColumn。

如果你想在 TableCell 中设置一个单独的组合框,你需要使用单元格工厂来包含它。请注意,每个 TableCell 都有自己的组合框(不是传递给所有单元格的单个组合框)。

@Override
public void start(Stage primaryStage) {
    StackPane root = new StackPane();
    Scene scene = new Scene(root, 300, 250);
    ObservableList<String> list = FXCollections.observableArrayList();
    list.add("Product One");
    list.add("Product Two");
    list.add("Product Three");
    list.add("Product Four");

    TableView table = new TableView();
    table.setEditable(true);
    TableColumn productName = new TableColumn("Product Name");
    TableColumn productCode = new TableColumn("Product Code");

    productName.setEditable(true);
    //productName.setCellValueFactory(ComboBoxTableCell.forTableColumn(list));
    productName.setCellFactory(new Callback<TableColumn, TableCell>() {
        @Override
        public TableCell call(TableColumn param) {
            return new TableCell() {
                private ComboBox comboBox;

                @Override
                protected void updateItem(Object item, boolean empty) {
                    super.updateItem(item, empty);
                    if (!empty) {
                        setGraphic(getComboBox());
                    } else {
                        setGraphic(null);
                    }
                }

                public ComboBox getComboBox() {
                    if (comboBox == null) {
                        comboBox = new ComboBox();
                        comboBox.setEditable(true);
                        comboBox.setItems(list);
                        TextFields.bindAutoCompletion(comboBox.getEditor(), comboBox.getItems());
                    }
                    return comboBox;
                }
            };
        }
    });

    productName.setMinWidth(100);
    productCode.setMinWidth(100);

    table.getColumns().addAll(productName, productCode);
    table.getItems().add(new Object[]{null, null});

    root.getChildren().add(table);

    primaryStage.setTitle("Test");
    primaryStage.setScene(scene);
    primaryStage.show();
}