【问题标题】:Put whole ComboBox in TableView column将整个 ComboBox 放在 TableView 列中
【发布时间】:2020-02-02 02:57:04
【问题描述】:

The Java Swing run imageThe JavaFx run image我需要将整个 ComboBox 添加到 TableView 列,而不是 JavaFx 中的 OservableArrayList,因为我像 java swing 一样为 ComboBox 设置了自动完成属性,所以我使用 java swing 进行了尝试,它工作成功,但没有t在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();
}

【问题讨论】:

  • 为什么要在不同的标签下重新发布完全相同的问题(而不是编辑the old)?您唯一的改变是使用正确的参数...
  • 好的,谢谢,我忘了删除旧的

标签: java combobox tableview javafx-8 editor


【解决方案1】:

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

如果要在 TableCell 中设置单独的组合框,则需要使用单元工厂将其包含在内。请注意,每个 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();
}

【讨论】:

  • 不要将轮子重新发明为八边形(或更糟;) - 外行,您缺少将组合的值设置为项目
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-03
  • 2016-12-15
  • 2021-05-02
  • 1970-01-01
  • 2014-02-11
  • 1970-01-01
相关资源
最近更新 更多