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();
}
答案 0 :(得分: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();
}