我有一个包含组合单元格的表,您可以从每个行条目中选择一个值。问题是,下拉列表仅显示正在编辑单元格的时间,并且我希望它始终显示在每一行上。这是可能的,如果是的话,怎么样?
TableColumn<Product, String> actionsCol = new TableColumn<Product, String>("Exception Reason");
actionsCol.setCellValueFactory(new PropertyValueFactory("exceptionReason"));
actionsCol.setCellFactory(ComboBoxTableCell.forTableColumn(exceptionReasons));
actionsCol.setOnEditCommit(
new EventHandler<CellEditEvent<Product, String>>() {
@Override
public void handle(CellEditEvent<Product, String> t) {
((Product) t.getTableView().getItems().get(t.getTablePosition().getRow())).setExceptionReason(t.getNewValue());
};
});
actionsCol.setEditable(true);
答案 0 :(得分:3)
您可以通过实现自己的表格单元格(并设置单元格工厂来实例化它)来执行此操作:
import java.util.Random;
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
public class TableWithComboBoxExample extends Application {
@Override
public void start(Stage primaryStage) {
TableView<Item> table = new TableView<>();
TableColumn<Item, String> itemCol = new TableColumn<>("Item");
itemCol.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
TableColumn<Item, Reason> reasonCol = new TableColumn<>("Reason");
reasonCol.setCellValueFactory(cellData -> cellData.getValue().importanceProperty());
reasonCol.setCellFactory(tc -> {
ComboBox<Reason> combo = new ComboBox<>();
combo.getItems().addAll(Reason.values());
TableCell<Item, Reason> cell = new TableCell<Item, Reason>() {
@Override
protected void updateItem(Reason reason, boolean empty) {
super.updateItem(reason, empty);
if (empty) {
setGraphic(null);
} else {
combo.setValue(reason);
setGraphic(combo);
}
}
};
combo.setOnAction(e ->
table.getItems().get(cell.getIndex()).setImportance(combo.getValue()));
return cell ;
});
table.getColumns().add(itemCol);
table.getColumns().add(reasonCol);
Random rng = new Random();
for (int i = 1 ; i <= 100 ; i++) {
table.getItems().add(new Item("Item "+i, Reason.values()[rng.nextInt(Reason.values().length)]));
}
primaryStage.setScene(new Scene(table));
primaryStage.show();
}
public enum Reason { DATA_NOT_OBTAINABLE, IM_JUST_PLAIN_LAZY, I_CANT_BE_BOTHERED }
public static class Item {
private final StringProperty name = new SimpleStringProperty();
private final ObjectProperty<Reason> importance = new SimpleObjectProperty<>();
public Item(String name, Reason importance) {
setName(name);
setImportance(importance);
}
public final StringProperty nameProperty() {
return this.name;
}
public final String getName() {
return this.nameProperty().get();
}
public final void setName(final String name) {
this.nameProperty().set(name);
}
public final ObjectProperty<Reason> importanceProperty() {
return this.importance;
}
public final Reason getImportance() {
return this.importanceProperty().get();
}
public final void setImportance(final Reason importance) {
this.importanceProperty().set(importance);
}
}
public static void main(String[] args) {
launch(args);
}
}