将整个组合框放在TableView列中

问题描述 投票:0回答:1

[The Java Swing run image The JavaFx run image我需要将整个ComboBox添加到TableView列,而不是JavaFx中的OservableArrayList,因为我像Java swing一样将autocomplete属性设置为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();
}
java combobox tableview javafx-8 editor
1个回答
0
投票

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

如果要在TableCell中设置单独的comboBox,则需要使用单元格工厂将其包括在内。请注意,每个TableCell将具有其自己的ComboBox(而不是传递给所有单元格的单个comboBox)。

@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();
}
© www.soinside.com 2019 - 2024. All rights reserved.